当使用 Coroutine 和 Retrofit 来调用网络 api 时,如果服务器返回错误(响应代码!= 200),我们将收到异常。我的问题是如何查找/读取服务器通过异常发送的错误主体(Json 格式)?
try{
apiService.login()
}catch(exception:Exception){
//How to read error body from exception
//Error body example: {"ok":false,"error":"Incorrect username or password."}
}
Run Code Online (Sandbox Code Playgroud)