Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xfy-c opened this issue Dec 2, 2021 · 6 comments

Comments

@xfy-c
Copy link

xfy-c commented Dec 2, 2021

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

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

@kbq670554802
Copy link

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

@ldlywt
Copy link
Owner

ldlywt commented Dec 16, 2021

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

@xfy-c
Copy link
Author

xfy-c commented Dec 17, 2021

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

@ruirui1128
Copy link

ruirui1128 commented Sep 22, 2022

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

@douhaopeng
Copy link

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

@Yanruplus
Copy link

用泛型啊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants