loopj RequestHandle在取消时崩溃

时间:2014-09-04 13:19:27

标签: android android-asynctask loopj asynchttpclient

我的Android应用程序中有一个loopj异步请求。

以下是代码:

private RequestHandle requestFetchAds;
requestFetchAds = HJRestClient.post(getActivity(), encodedURL, params, new JsonHttpResponseHandler() {...});

请求执行正常,在某些情况下我需要取消请求。 这是取消代码:

if (requestFetchAds != null && !requestFetchAds.isFinished() && !requestFetchAds.isCancelled()) {
            Log.d(LOG_TAG, String.format("Cancelling request fetch ads"));
            Log.d(LOG_TAG, String.format("Cancelling request: ") + (requestFetchAds.cancel(true) ? " succeeded" : " failed"));
}

但它在方法requestFetchAds.cancel(true)上崩溃了,我不知道为什么。

这是日志:

09-04 16:14:42.530    3999-3999/com.haraj.haraj E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1144)
            at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.close(OpenSSLSocketImpl.java:996)
            at org.apache.http.impl.SocketHttpClientConnection.shutdown(SocketHttpClientConnection.java:183)
            at org.apache.http.impl.conn.DefaultClientConnection.shutdown(DefaultClientConnection.java:150)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.shutdown(AbstractPooledConnAdapter.java:169)
            at org.apache.http.impl.conn.AbstractClientConnAdapter.abortConnection(AbstractClientConnAdapter.java:378)
            at org.apache.http.client.methods.HttpRequestBase.abort(HttpRequestBase.java:159)
            at com.loopj.android.http.AsyncHttpRequest.cancel(AsyncHttpRequest.java:169)
            at com.loopj.android.http.RequestHandle.cancel(RequestHandle.java:50)
            at com.haraj.haraj.HJAdsListFragment.reloadAds(HJAdsListFragment.java:699)
            at com.haraj.haraj.HJAdsListFragment$2.onClick(HJAdsListFragment.java:328)
            at android.view.View.performClick(View.java:4475)
            at android.view.View$PerformClick.run(View.java:18786)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5493)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
            at dalvik.system.NativeStart.main(Native Method)

我也尝试使用上下文取消请求:

HJRestClient.getHTTPClient().cancelRequests(getActivity(), true);

但这也没有帮助。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

我可以通过将取消请求包装在AsyncTask中来解决崩溃问题,如下所示。

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            Log.d(LOG_TAG, String.format("Cancelling request: ") + (requestFetchAds.cancel(false) ? " succeeded" : " failed"));

        }
    });

感谢Selvin。