8

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 anything. This part is expected, but it also doesn't fire the onFailure method.

Also, the code I have used when there is an internet connection does work so there is no problem on the server end.

Here is some code that is stripped down to the minimum. It also doesn't work (I have tested this too)

String url = getString(R.string.baseurl) + "/appconnect.php";
client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
client.get(url, null, new JsonHttpResponseHandler()
{
    @Override
    public void onSuccess(JSONArray response)
    {
        Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(Throwable e, JSONArray errorResponse)
    {
        Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
    }
});

Thanks, Ashley

twaddington
  • 11,607
  • 5
  • 35
  • 46
Ashley Staggs
  • 1,557
  • 9
  • 24
  • 38

3 Answers3

7

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

@Override
public void onFailure(Throwable e) {
    Log.e(TAG, "OnFailure!", e);
}
@Override
public void onFailure(Throwable e, String response) {
    Log.e(TAG, "OnFailure!", e);
}
@Override
public void onFailure(Throwable e, JSONArray errorResponse) {
    Log.e(TAG, "OnFailure!", e);
}
twaddington
  • 11,607
  • 5
  • 35
  • 46
7

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);
        }
    }
Baz
  • 36,440
  • 11
  • 68
  • 94
nicous
  • 411
  • 2
  • 5
  • This is the solution! It's been [merged](https://github.com/loopj/android-async-http/commit/f854f62633726ab38d2795d0c1e805212a4f0d76) by loopj! Nice one nicous! – dbro Dec 05 '12 at 00:02
  • Hi nicous, how can we edit AsyncHttpRequest.class Please help . Is there any way to edit this .class file inside the jar file – Mathew M Visacanthara Jun 16 '15 at 15:31
0

Try this:

@Override
protected Object parseResponse(byte[] responseBody) throws JSONException {
    return super.parseResponse(responseBody);
}
tier777
  • 1,005
  • 12
  • 11