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?

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.