简体   繁体   中英

Need to get the response message, while getting 400 error in Retrofit, Android

I'm getting the following response, while 400 error occurs.

  {"unit":"You may not create job for other than your unit!"}

    2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: <-- 400 https://myserver.com/v1/jobs/ (131ms)
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: date: Tue, 08 Jun 2021 11:45:35 GMT
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: content-type: application/json
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: content-length: 59
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: server: nginx/1.18.0 (Ubuntu)
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: vary: Accept, Origin
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: allow: GET, POST, HEAD, OPTIONS
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: x-frame-options: DENY
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: x-content-type-options: nosniff
2021-06-08 17:15:33.715 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: referrer-policy: same-origin
2021-06-08 17:15:33.716 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: {"unit":"You may not create job for other than your unit!"}
2021-06-08 17:15:33.716 8599-8752/com.fyxtpmtenant I/okhttp.OkHttpClient: <-- END HTTP (59-byte body)

I need to show as an alert to the user, what ever I get in the 400 error response. The response may change. I tried the following

 JSONObject msgObj = new JSONObject(String.valueOf(response.raw()));

But get the following error

org.json.JSONException: Value Response of type java.lang.String cannot be converted to JSONObject

Edit: I tried the following to get the response:

 Log.e("error body 1>",response.errorBody()+" ");
  Log.e("error body 2>",response.body()+" ");
  Log.e("error body 3>",response.toString()+" ");
  Log.e("error body 4>",response.message()+" ");
  Log.e("error body 8>",response.raw()+" ");

And got this:

error body 1>: okhttp3.ResponseBody$Companion$asResponseBody$1@d2d0fcc 
error body 2>: null 
error body 3>: Response{protocol=h2, code=400, message=, url=https://c1.myserver.com/v1/jobs/} 
error body 4>:  
error body 8>: Response{protocol=h2, code=400, message=, url=https://c1.myserver.com/v1/jobs/} 

Try to use this to map your response to the format of the error supplied by the API:

public static ErrorDTO parseError(Retrofit retrofit, Response<?> response) {
    Converter<ResponseBody, ErrorDTO> converter = retrofit.responseBodyConverter(ErrorDTO.class, new Annotation[0]);

    ErrorDTO error;

    try {
        error = converter.convert(response.errorBody());
    } catch (IOException e) {
        return new ErrorDTO();
    }

    return error;
}

ErrorDTO should be a simple POJO which matches the structure of the body of the error response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM