Retrofit 關(guān)于Gson 碰到坑求救

一、配置
//RxJava/RxAndroid
compile'io.reactivex.rxjava2:rxjava:2.0.1'
compile'io.reactivex.rxjava2:rxandroid:2.0.1'
//retrofit
compile'com.squareup.retrofit2:retrofit:2.1.0'
//Gson converter
compile'com.squareup.retrofit2:converter-gson:2.1.0'
/* compile('org.ligboy.retrofit2:converter-fastjson:2.1.0') {
exclude module: 'fastjson'
}*/
//RxJava2 Adapter
compile'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
//okhttp
compile'com.squareup.okhttp3:okhttp:3.4.1'
compile'com.squareup.okhttp3:logging-interceptor:3.4.1'

二、服務(wù)器返回格式
{
error:"0",
content:{},
message:"success"
}
三、定義Bean
public class HttpResult{
private String message;
private String error;
private String content;//相關(guān)數(shù)據(jù)
}
四、API
@FormUrlEncoded
@POST("mobile/user/login")
Observable<HttpResult> login(@Field("key")String key,@Field("imei")String imei,@Field("isInit")String isInit);
五、運(yùn)行拋異常
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 3 column 16 path $.content

六、泛型問題
將HttpResult改成
public class HttpResult<T>{
private String message;
private String error;
private T content;//相關(guān)數(shù)據(jù)
}
將Api改成
@FormUrlEncoded
@POST("mobile/user/login")
Observable<HttpResult<User>> login(@Field("key")String key,@Field("imei")String imei,@Field("isInit")String isInit);

運(yùn)行拋異常**
com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: Invalid double: ""**
嘗試以下配置依舊拋異常
GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss");**
gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer() {
public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
return new Date(json.getAsJsonPrimitive().getAsLong());
}
});
gsonBuilder.serializeNulls();
Gson gson = gsonBuilder.create();

七、泛型不確定類型 拋異常
[泛型解析遇到com.google.gson.internal.LinkedTreeMap cannot be cast to object]

八、臨時解決辦法
將compile'com.squareup.retrofit2:retrofit:2.1.0' //Gson converter
換成
compile'com.squareup.retrofit2:converter-gson:2.1.0' //fastjson 可以解決

九、再此望各位有解決辦法的不吝指教!萬分感謝!!

十、(20170405補(bǔ)充)已找問題所在,是由于服務(wù)器返回的是非標(biāo)準(zhǔn)Gson格式(包含換行符)
解決辦法1.參考八;2.返回Responsebody自行解析;3.讓服務(wù)器修改返回標(biāo)準(zhǔn)Gson字符串格式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,881評論 25 709
  • 新項目由自己重頭架構(gòu),因此使用MVP模式,庫的使用上使用了Rxjava 2.0,OkHttp 3.0,Retr...
    殘霜丶閱讀 583評論 0 1
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評論 19 139
  • 2015的冬天,在我把自己關(guān)進(jìn)房子的很多天后,父親帶我去了醫(yī)院-抑郁癥。沒有人意外,這是我們早在心里就確定過的結(jié)論...
    梅阿查閱讀 355評論 0 0
  • 2017/4/22成長小組,來了一位新朋友A,剛見到她的時候覺得她開朗,利落,干脆,能干。熟悉了環(huán)境之后,她開始講...
    VivyYNSH閱讀 686評論 0 0

友情鏈接更多精彩內(nèi)容