Loopj Android Async Http - onFailure not running

I am using a large asynchronous http library from loopj, but I ran into a small error.

If the user does not have an Internet connection or is not connected, the application simply will not return anything. This part is expected, but it also does not run the onFailure method.

In addition, the code that I used when connecting to the Internet works, so there are no problems on the server.

Here is some code that is split to a minimum. It also doesn't work (I experienced it 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

+8
java android asynchronous loopj
source share
3 answers

You can try the following:

In AsyncHttpRequest->makeRequestWithRetries() add catch to SocketException as follows:

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

Yes, unfortunately, the loopj Android library is not very well designed. If you make other onFailure , one of them should run:

 @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); } 
+7
source

Try the following:

 @Override protected Object parseResponse(byte[] responseBody) throws JSONException { return super.parseResponse(responseBody); } 
0
source

All Articles