DominicM
DominicM

Reputation: 2196

Android: Loopj AsyncHttpClient Get HTTP Error Code

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?

Upvotes: 1

Views: 3259

Answers (2)

Amandeep Rohila
Amandeep Rohila

Reputation: 3588

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

Upvotes: 0

Sangharsh
Sangharsh

Reputation: 3029

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);
}

Upvotes: 5

Related Questions