1

I'm using Loopj's AsyncHttpClient to do http requests with JSON data. Now I need to get the http error code that is returned from the server. Where can I find that? The handler that I pass looks like this:

new JsonHttpResponseHandler()
{
    @Override
    public void onFailure(Throwable throwable, JSONObject object)
    {
        // Get error code
    }
}

Is this not possible at all?

DominicM
  • 2,186
  • 5
  • 24
  • 42

2 Answers2

5

Try this:

HttpResponseException hre = (HttpResponseException) throwable;
int statusCode = hre.getStatusCode();

It should work only for status code >= 300, because of following code in AysncHttpResponseHandler class in loopj

if(status.getStatusCode() >= 300) {
     sendFailureMessage(new HttpResponseException(status.getStatusCode(), status.getReasonPhrase()), responseBody);
}
Sangharsh
  • 2,999
  • 2
  • 15
  • 27
0

Give a look at client.post(null, url, entity, "application/json", responseHandler);

Declare method as given in below and you will you will get status code

RestClientAvaal.post(url, entity, new BaseJsonHttpResponseHandler<>() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, String rawJsonResponse, Object response) {

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, Throwable throwable, String rawJsonData, Object errorResponse) {

            }

            @Override
            protected Object parseResponse(String rawJsonData, boolean isFailure) throws Throwable {
                return null;
            }
        });

And if you want to get exception message then try new Exception(throwable).getMessage() in onFailure method

Amandeep Rohila
  • 3,558
  • 2
  • 28
  • 34