java Loopj Android Async Http - onFailure 未触发
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12866353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loopj Android Async Http - onFailure not fired
提问by Ashley Staggs
I am using the great async http library from loopj, but I have run into a small snag.
我正在使用 loopj 中出色的异步 http 库,但遇到了一个小问题。
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.
如果用户没有互联网连接或失去连接,该应用程序将不会返回任何内容。这部分是预期的,但它也不会触发 onFailure 方法。
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
谢谢,阿什利
采纳答案by nicous
You can try this:
你可以试试这个:
In AsyncHttpRequest->makeRequestWithRetries()
, add a catch to SocketException
like this:
在 中AsyncHttpRequest->makeRequestWithRetries()
,添加一个SocketException
像这样的捕获:
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);
}
}
回答by twaddington
Yeah, unfortunately the loopj Android library isn't very well designed. If you implement the otheronFailure
callbacks one of them should fire:
是的,不幸的是,loopj Android 库设计得不是很好。如果您实现其他onFailure
回调,其中一个应该触发:
@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);
}
回答by tier777
Try this:
试试这个:
@Override
protected Object parseResponse(byte[] responseBody) throws JSONException {
return super.parseResponse(responseBody);
}