【问题标题】:Loopj Android Async Http - onFailure not firedLoopj Android Async Http - onFailure 未触发
【发布时间】:2012-10-12 20:08:24
【问题描述】:

我正在使用来自 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();
    }
});

谢谢, 阿什利

【问题讨论】:

    标签: java android http asynchronous loopj


    【解决方案1】:

    你可以试试这个:

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

    【讨论】:

    • 这就是解决方案! loopj 一直是merged!不错不错!
    • 您好,我们如何编辑 AsyncHttpRequest.class 请帮助。有没有办法在jar文件中编辑这个.class文件
    【解决方案2】:

    是的,不幸的是 loopj Android 库的设计不是很好。如果你实现 other 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);
    }
    

    【讨论】:

    • 我尝试了这些以及 JSONObject 失败。不幸的是,它仍然无法正常工作。
    【解决方案3】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-20
      • 2018-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多