如何通过 kotlin 中的 retrofit2 获取 json
how get json by retrofit2 in kotlin
我使用 retrofit2 连接到 api 并接收数据
数据嵌套
D/OkHttp: {"success":{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9}}
如果数据如上例,则接收无问题
但是如果是如下,则报错
D/OkHttp: {"message":"Unauthenticated."}
我为第一个例子这样做
data class Response(var success : AutoLoginUser)
data class AutoLoginUser(var token: String)
...
...
...
Log.w("OK FROM SERVER", response.body()!!.success!!.token)
对于第二个示例,我执行以下操作
data class UserDetails(var message : String)
...
...
...
Log.w("OK FROM SERVER", response.body()!!.message)
第二个例子的错误
D/OkHttp: <-- 401 Unauthorized http://192.168.1.8/api/details (252ms)
Host: 192.168.1.8
Date: Wed, 02 Dec 2020 09:01:03 GMT
Connection: close
X-Powered-By: PHP/7.4.12
Cache-Control: no-cache, private
Date: Wed, 02 Dec 2020 09:01:03 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
D/OkHttp: {"message":"Unauthenticated."}
<-- END HTTP (30-byte body)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ir.i9i.splendor, PID: 5740
java.lang.NullPointerException
at ir.i9i.splendor.Profile$getUserDetails.onResponse(Profile.kt:39)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
您可以在 Retrofit
的 onResponse()
方法中使用 errorBody()
处理它:
if (response.code() == 200 && response.isSuccessful) {
// do whatever you want when the response is 200 and isSuccessful
} else {
if (response.errorBody() != null) {
val jsonObj =
JSONObject(response.errorBody()!!.charStream().readText())
val getMessage = jsonObj.getString("message")
// Here get the message and print it out
showSnackBar(yourActivity, getMessage)
}
}
因为您检索了 401
错误响应,您将能够在 onResponse()
方法中处理此类错误。
我使用 retrofit2 连接到 api 并接收数据 数据嵌套
D/OkHttp: {"success":{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9}}
如果数据如上例,则接收无问题 但是如果是如下,则报错
D/OkHttp: {"message":"Unauthenticated."}
我为第一个例子这样做
data class Response(var success : AutoLoginUser)
data class AutoLoginUser(var token: String)
...
...
...
Log.w("OK FROM SERVER", response.body()!!.success!!.token)
对于第二个示例,我执行以下操作
data class UserDetails(var message : String)
...
...
...
Log.w("OK FROM SERVER", response.body()!!.message)
第二个例子的错误
D/OkHttp: <-- 401 Unauthorized http://192.168.1.8/api/details (252ms)
Host: 192.168.1.8
Date: Wed, 02 Dec 2020 09:01:03 GMT
Connection: close
X-Powered-By: PHP/7.4.12
Cache-Control: no-cache, private
Date: Wed, 02 Dec 2020 09:01:03 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
D/OkHttp: {"message":"Unauthenticated."}
<-- END HTTP (30-byte body)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ir.i9i.splendor, PID: 5740
java.lang.NullPointerException
at ir.i9i.splendor.Profile$getUserDetails.onResponse(Profile.kt:39)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
您可以在 Retrofit
的 onResponse()
方法中使用 errorBody()
处理它:
if (response.code() == 200 && response.isSuccessful) {
// do whatever you want when the response is 200 and isSuccessful
} else {
if (response.errorBody() != null) {
val jsonObj =
JSONObject(response.errorBody()!!.charStream().readText())
val getMessage = jsonObj.getString("message")
// Here get the message and print it out
showSnackBar(yourActivity, getMessage)
}
}
因为您检索了 401
错误响应,您将能够在 onResponse()
方法中处理此类错误。