Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

okhttp get failure response

I've implemented okhttp in my android client for network calls.

When i get a failure response i get the failure code and the text related to the code as a message but i don't get the custom failure response that the server sends me. In my failure response in the implemented code the message i get is just "Bad Request".

Whereas the same response from the browser is as follows. enter image description here

How do i get the error message the server is giving me back?

My code

private void executeCall(Request request, final ResponseListener listener) {
        mOKHttpClient.newCall(request)
                     .enqueue(new Callback() {
                         @Override
                         public void onFailure(Call call, IOException e) {                              
                             postFailure(listener, (String) call.request()
                                                                .tag(),e.toString());
                         }

                         @Override
                         public void onResponse(Call call, final Response response) throws IOException {
                             if(response.isSuccessful()) {
                                 String responseString = response.body().string();                                   
                                 postSuccess(listener, (String)call.request().tag(), responseString);
                             }
                             else {                                 
                                 postFailure(listener, (String)call.request().tag(),response.code()+","+response.message());
                             }
                         }
                     });
    }

Here's my response in case of failure. enter image description here

like image 372
ViVekH Avatar asked Aug 30 '16 04:08

ViVekH


People also ask

How do you get response body from OkHttp response?

OkHttp Response To implement our JSON decoder, we need to extract the JSON from the result of the service call. For this, we can access the body via the body() method of the Response object.

Is OkHttp blocked?

OkHttp Overview At a high level, the client is designed for both blocking synchronous calls and nonblocking asynchronous calls.

Is OkHttp asynchronous?

OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.


2 Answers

You will have to catch error response by body() because response.message() returns HTTP status message.

In the screen shot provided by you:

Status is broken down in OkHttp like response.code() for HTTP status code which is 400 in your case and response.message() for HTTP status message which is Bad Request in your case.

The body of the response (be it success or failure) is response.body(). And if you want to get it as a String, then call response.body().string().

@Override
public void onResponse(Call call, final Response response) throws IOException {
     if(response.isSuccessful()) {
         String responseString = response.body().string();                                   
         postSuccess(listener, (String)call.request().tag(), responseString);
     }
     else {               
        String errorBodyString = response.body().string();                  
        postFailure(listener, (String)call.request().tag(),response.code()+","+errorBodyString);
     }
}

As per comments:

Since you want to read Message object from the response, try to do like this:

JSONObject object = new JSONObject (response.body().string());
String messageString = object.getString("Message");
like image 143
Rohit Arya Avatar answered Oct 04 '22 19:10

Rohit Arya


if you are trying to get (okhttp3.ResponseBody) errorResponse from Retrofit response do this..

                // Retrofit onResponseCall
                @Override
                public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {

                    if (response.errorBody() != null) {

                        try {
                            Log.e("errorResponse","" + response.errorBody().toString());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                     }
                }
                   
like image 44
Adarsh Vijayan P Avatar answered Oct 04 '22 19:10

Adarsh Vijayan P