Loopj AndroidasynchronousHttp – onFailure未解雇

我使用来自loopj的伟大的asynchronoushttp库,但我遇到了一个小的障碍。

如果用户没有互联网连接或失去他们的连接,应用程序将不会返回任何东西。 这部分是预期的,但它也不会触发onFailure方法。

此外,我有一个互联网连接时使用的代码工作,所以没有问题的服务器端。

这是一些代码被剥离到最低限度。 这也行不通(我也testing过了)

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

谢谢,阿什利

Solutions Collecting From Web of "Loopj AndroidasynchronousHttp – onFailure未解雇"

你可以试试这个:

AsyncHttpRequest->makeRequestWithRetries() ,像下面这样向SocketException添加一个catch:

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

是的,不幸的是loopj Android库的devise不是很好。 如果你实现其他 onFailurecallback其中一个应该触发:

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

尝试这个:

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