Loopj Android Async Http - onFailure not fired

[亡魂溺海] 提交于 2019-12-03 13:13:07
nicous

You can try this:

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

Yeah, unfortunately the loopj Android library isn't very well designed. If you implement the other onFailure callbacks one of them should fire:

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

Try this:

@Override
protected Object parseResponse(byte[] responseBody) throws JSONException {
    return super.parseResponse(responseBody);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!