Skip to content Skip to sidebar Skip to footer

Loopj Android Async Http - Onfailure Not Fired

I am using the great async http library from loopj, but I have run into a small snag. If the user has no internet connection or loses their connection, the app just won't return an

Solution 1:

You can try this:

In AsyncHttpRequest->makeRequestWithRetries(), add a catch to SocketException like this:

while (retry) {
        try {
            makeRequest();
            return;
        } catch (UnknownHostException e) {
            if(responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (SocketException e){
            // Added to detect no connection.if(responseHandler != null) {
                responseHandler.sendFailureMessage(e, "can't resolve host");
            }
            return;
        } catch (IOException e) {
            cause = e;
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        } catch (NullPointerException e) {
            // there's a bug in HttpClient 4.0.x that on some occasions causes// DefaultRequestExecutor to throw an NPE, see// http://code.google.com/p/android/issues/detail?id=5255
            cause = new IOException("NPE in HttpClient" + e.getMessage());
            retry = retryHandler.retryRequest(cause, ++executionCount, context);
        }
    }

Solution 2:

Yeah, unfortunately the loopj Android library isn't very well designed. If you implement the otheronFailure callbacks one of them should fire:

@OverridepublicvoidonFailure(Throwable e) {
    Log.e(TAG, "OnFailure!", e);
}
@OverridepublicvoidonFailure(Throwable e, String response) {
    Log.e(TAG, "OnFailure!", e);
}
@OverridepublicvoidonFailure(Throwable e, JSONArray errorResponse) {
    Log.e(TAG, "OnFailure!", e);
}

Solution 3:

Try this:

@Overrideprotected Object parseResponse(byte[] responseBody)throws JSONException {
    returnsuper.parseResponse(responseBody);
}

Post a Comment for "Loopj Android Async Http - Onfailure Not Fired"