FastJetpack
FastJetpack copied to clipboard
能处理一下返回的数据结构不一致的问题吗?有时候后台返回成功的数据是对象,失败的数据是数组这种怎么处理?
失败返回的数据: { "status_code": 9218, "message": "密码错误,剩余4次输入", "data": [] }
成功返回的数据: { "status_code": 1000, "message": "成功", "data": { "id_no": "BtLhO2qmaOrvQSA5xbYTVqOJGJtd/oHkvIkTChI3c3c=", "is_verifyed": 0, "phone": "c59le6L65XK1htExLNic/w==", } } 我重写你那个Gson解析器,抛ApiFailedResponse这个函数一直有错,大佬能帮忙看下吗?
这个时候干后端就对了,何必为难自己
你可以跟后端讨论下什么是 RESTFUL...
他会跟你讨论下为啥iOS可以,android不行
这个问题,本身是后台不规范导致的问题。如果他们不愿意改,你可以在拦截器中直接把data,字段改掉。
class ResponseInterceptor : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val response = chain.proceed(chain.request())
if (response.body != null) {
try {
var body = response.body?.string()
val model = Gson().fromJson(body, Resp::class.java)
if (model.code == 9218) {
body = body?.replace("\"data\":", "\"datas\":")
}
val responseBody = body?.toResponseBody()
return response.newBuilder().body(responseBody).build()
} catch (e: Exception) {
}
return response
} else {
return response
}
}
}
老哥这个问题怎么解决啊 我也遇到了
用泛型啊