循环J Android的异步的Http - onFailure不会被触发 [英] Loopj Android Async Http - onFailure not fired

查看:244
本文介绍了循环J Android的异步的Http - onFailure不会被触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的循环J伟大的异步HTTP库,但我遇到了一个小的障碍。

I am using the great async http library from loopj, but I have run into a small snag.

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

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.

此外,code我用的时候有一个互联网连接不工作所以在服务器端。

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

下面是剥离下来到最小一些code。它也没有工作(我已经测试了这一点)

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

推荐答案

您可以试试这个:

AsyncHtt prequest-> makeRequestWithRetries(),添加一个catch到 SocketException 是这样的:

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

这篇关于循环J Android的异步的Http - onFailure不会被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆