FastJetpack icon indicating copy to clipboard operation
FastJetpack copied to clipboard

能处理一下返回的数据结构不一致的问题吗?有时候后台返回成功的数据是对象,失败的数据是数组这种怎么处理?

Open xfy-c opened this issue 3 years ago • 6 comments

失败返回的数据: { "status_code": 9218, "message": "密码错误,剩余4次输入", "data": [] }

成功返回的数据: { "status_code": 1000, "message": "成功", "data": { "id_no": "BtLhO2qmaOrvQSA5xbYTVqOJGJtd/oHkvIkTChI3c3c=", "is_verifyed": 0, "phone": "c59le6L65XK1htExLNic/w==", } } 我重写你那个Gson解析器,抛ApiFailedResponse这个函数一直有错,大佬能帮忙看下吗?

xfy-c avatar Dec 02 '21 10:12 xfy-c

这个时候干后端就对了,何必为难自己

kbq670554802 avatar Dec 16 '21 07:12 kbq670554802

你可以跟后端讨论下什么是 RESTFUL...

ldlywt avatar Dec 16 '21 15:12 ldlywt

他会跟你讨论下为啥iOS可以,android不行

xfy-c avatar Dec 17 '21 02:12 xfy-c

这个问题,本身是后台不规范导致的问题。如果他们不愿意改,你可以在拦截器中直接把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
        }
    }
}

ruirui1128 avatar Sep 22 '22 01:09 ruirui1128

老哥这个问题怎么解决啊 我也遇到了

douhaopeng avatar Jul 21 '23 18:07 douhaopeng

用泛型啊

Yanruplus avatar Sep 03 '23 12:09 Yanruplus