gpt4 book ai didi

java - Loopj Android Async Http - onFailure 未触发

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:58:56 25 4
gpt4 key购买 nike

我正在使用来自 loopj 的出色异步 http 库,但我遇到了一个小问题。

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

此外,我在有互联网连接时使用的代码确实有效,因此在服务器端没有问题。

这里是一些被精简到最少的代码。它也不起作用(我也测试过)

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

谢谢,阿什莉

最佳答案

你可以试试这个:

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

关于java - Loopj Android Async Http - onFailure 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12866353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com