【发布时间】:2020-08-03 05:32:20
【问题描述】:
我在使用 Retrofit+Gson+RxJava 时遇到了一些奇怪的行为
这是我的改装对象
Retrofit.Builder()
.baseUrl(Constants.Urls.URL_BASE)
.addConverterFactory(GsonConverterFactory.create(Gson()))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build()
这是我的数据类
data class User(
val id:Int,
val email:String,
val name:String)
这是我的改造界面
@Multipart
@POST(Constants.Urls.URL_LOGIN)
fun makeLogin(@PartMap map: Map<String, String?>): Observable<Model_User>
当登录成功时,一切正常,但是当我得到字符串错误而不是响应中的 json 对象时,我得到了奇怪的行为,例如
{
"error": {
"code": 400,
"message": "Wrong password"
}
}
Observable 使用 User 对象调用订阅成功。并且该对象在不能为空的字段上具有空值。
my_api.makeLogin(map)
.subscribe(
{
//Here i got User(id = null,email = null,name = null)
},
{
//But i need to call error here on parsing failed
})
在调用 onNext 之前我应该怎么做才能抛出错误?当 Gson 解析失败而不是发出空对象时,是否可以进行改造抛出错误?
【问题讨论】:
标签: android kotlin gson retrofit rx-java