1

I have a loopj async request in my android app.

Here's the code:

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

The request executes fine and in some situations I need to cancel the request. Here's the cancelling code:

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

But it crashes on the method requestFetchAds.cancel(true) and I don't know why.

Here's the log:

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)

I also had tried cancelling the request using the context like:

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

But this also did not help.

Any help would be appreciated.

Selvin
  • 6,598
  • 3
  • 37
  • 43
Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65
  • `But it crashes on the method requestFetchAds.cancel(true) and I don't know why.` because of android.os.NetworkOnMainThreadException ... – Selvin Sep 04 '14 at 13:28
  • @Selvin what does that mean? I honestly don't know. I'm a beginner in android development. – Abdullah Umer Sep 04 '14 at 13:30
  • honestly, [google.com](http://google.com) should know (or [android framework documentation](http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html)) – Selvin Sep 04 '14 at 13:30
  • @Selvin thanks for helping. http://stackoverflow.com/a/6343299/1478181 says that "This exception is thrown when an application attempts to perform a networking operation on its main thread." but according to my understanding loopj's AsyncHttpClient runs AsyncTask. – Abdullah Umer Sep 04 '14 at 13:39
  • it seems like only request is using differnt thread and canceling not ... – Selvin Sep 04 '14 at 13:44
  • @Selvin okay, thanks for narrowing it down. Any suggestions on how to run the cancel request on the same thread? – Abdullah Umer Sep 04 '14 at 13:46
  • wrap this call with AsyncTask or thread – Selvin Sep 04 '14 at 13:47

1 Answers1

2

I was able to fix the crash by wrapping the cancel request in an AsyncTask as follows.

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

        }
    });

Thanks to Selvin.

Abdullah Umer
  • 4,234
  • 5
  • 36
  • 65