Search code examples
androidhttploopj

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?


Solution

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