我对 Android 中的 Retrofit 有疑问,还没有找到解决方案。当我尝试调用 API 登录时,如果电话号码和密码正确,我可以获取数据,但是当其中一个主题错误时,我会从带有改造的 Android logcat 获得状态代码 422:
021-12-15 13:45:16.828 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: --> POST https://my-domain/auth/login
2021-12-15 13:45:16.829 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Content-Type: application/json; charset=UTF-8
2021-12-15 13:45:16.829 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Content-Length: 99
2021-12-15 13:45:16.830 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: {"device_id":"566130387dde6f8b","password":"abc123","phone":"0383074411","platform":"android"}
2021-12-15 13:45:16.830 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: --> END POST (99-byte body)
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: <-- 422 Unprocessable Entity https://my-domain/auth/login (145ms)
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Server: nginx/1.19.3
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Content-Type: application/json
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Transfer-Encoding: chunked
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Connection: keep-alive
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: X-Powered-By: PHP/7.4.20
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Cache-Control: no-cache, private
2021-12-15 13:45:16.977 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Date: Wed, 15 Dec 2021 06:45:16 GMT
2021-12-15 13:45:16.978 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: X-RateLimit-Limit: 1500
2021-12-15 13:45:16.978 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: X-RateLimit-Remaining: 1499
2021-12-15 13:45:16.978 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Vary: Origin
2021-12-15 13:45:16.978 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: Set-Cookie: boxsocial_session=eyJpdiI6IkVDaDg0UkhLR2lIN1B3N0wvODFuaXc9PSIsInZhbHVlIjoiNklNeTY2czgxWG5qdmFZTVlzQzhLVFF6M053cG9TNTdVdC9ZWjJDWTEzLytLZ3NoTituNVpVemQ3eEVkeEZyQmk0YVhhdVNsWndnZnFGMnhjeUF0WFgxZkFxQ2Qrem5XcEdKSUozalFVbDJobnZrVlJVV0hqSE02ZkpWZnhoQ0IiLCJtYWMiOiJkNDJkOWJiMzhiOGQ2YTI5Y2VmNWM0Mzc2OWZiOGRhYjU5NDE3MGRmYTA1ZTIyODNjOWUxMTgyYTk5YTAzMzUxIn0%3D; expires=Fri, 14-Jan-2022 06:45:16 GMT; Max-Age=2592000; path=/; domain=.bongdapro.vn; httponly; samesite=lax
2021-12-15 13:45:16.979 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: {"error_code":4,"message":"S\u1ed1 \u0111i\u1ec7n tho\u1ea1i kh\u00f4ng h\u1ee3p l\u1ec7"}
2021-12-15 13:45:16.979 14449-14541/com.boxvn.gglive.user I/okhttp.OkHttpClient: <-- END HTTP (90-byte body)
这是我在 Postman 中调用 API 时的响应
{
"error_code": 4,
"message": "The phone number is not correct"
}
但是在Android中,我无法从任何日志中获取消息,我尝试了太多方法但无法获取消息:电话号码不正确,就像邮递员一样。所以有人知道为什么吗?请帮我。非常感谢。
val response = service.loginWithPhone(body)
logger("response: $response")
// call API with Call
val call = service.loginWithPhoneNormal(body)
call.enqueue(object : Callback<JsonObject> {
override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {
Log.d("callAPI", "response: $response")
if (response.isSuccessful) {
val responseBody = response.body()
val code = response.code()
val message = response.message()
Log.wtf("onResponse", "responseBody: $responseBody")
Log.wtf("onResponse", "code: $code")
Log.wtf("onResponse", "message: $message")
} else {
val errorBody = response.errorBody()
Log.wtf("onResponse", "errorBody: ${response.errorBody()}")
Log.wtf("onResponse", "errorBody: ${errorBody.toString()}")
Log.wtf("onResponse", "errorBody: ${Gson().toJson(errorBody)}")
}
}
override fun onFailure(call: Call<JsonObject>, t: Throwable) {
Log.wtf("onFailure", "Throwable: ${t.localizedMessage}")
}
})
这是我的 API 服务:
@POST("auth/login")
fun loginWithPhoneNormal(@Body body: RequestBodies.LoginBody): Call<JsonObject>
@POST("auth/login")
suspend fun loginWithPhone(@Body body: RequestBodies.LoginBody): Response<JsonObject>