4

I have been searching about this but I did not find my specific question. I understand that AskyncTask can be canceled using .cancel(true) but this only happens if I have a loop in which I can check the value isCanceled().

But my question here is.. how can I cancel an AsyncTask (that is stuck in httpclient.execute() ) when the user presses back? If the user navigates away from that Activity and goes to another I dont want to have an uncontrolled number of AsyncTask running because this may lead to memory problems, the user could navigate back and forth and create undetermined number of tasks. That is why I want to close them. Anyone knows a way? I post the code I use to connect:

public class Test extends Activity {

@Override
protected void onStart() {
    super.onStart();

    new ConnectionTask().execute("https://www.mywebserver.com/webservice.php?param1=test");
}

private class ConnectionTask extends AsyncTask<String, Void, String>{

    @Override
    protected String doInBackground(String... params) {
        try {

            HttpClient httpclient = DefaultHttpClient(params,clientConnectionManager);
            HttpPost httpPost = new HttpPost(params[0]);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();

            if(httpEntity != null)
                return EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;        
    }

  }

}

Do you know what I should add in onStop() to cancel the ongoing httpClient.execute() function? Sometimes gets stuck almost forever.

I would really appreciate your help, thank you very much in advance.

UPDATE

If I close the ConnectionManager I have to do the handshaking for https again, right? Look at this code when I create the httpClient, I use this for https:

HttpClient httpclient = DefaultHttpClient(params,clientConnectionManager);

Thank you all for your quick responses and the variety of solutions exposed here. I will try to use the timeouts (for not having to wait too much) + the cancel() function to avoid processing onPostExecute. I will tell if the results are as expected! Thank you very much you all!

4
  • Have you tried to just cancel the ongoing thread? Sep 12, 2011 at 16:54
  • When you try to cancel AsyncTask, it just puts isCancelled() to true, there is no way I can destroy the AsyncTask (that I know), .cancel(true) doesnt work.
    – jesbomar
    Sep 12, 2011 at 16:58
  • Try this httpclient.getConnectionManager().shutdown(); Sep 12, 2011 at 17:08
  • I wrote http but it is https in fact. This code is a simplified version, because I would like to mantain the connectionmanager and the httpParams to avoid further hand-shakings, is there a way of avoiding shutting down the connectionManager?
    – jesbomar
    Sep 12, 2011 at 17:15

6 Answers 6

4

according to HttpClient docs, use HttpUriRequest#abort() to abort request

1.4. Aborting requests

In some situations HTTP request execution fails to complete within the expected time frame due to high load on the target server or too many concurrent requests issued on the client side. In such cases it may be necessary to terminate the request prematurely and unblock the execution thread blocked in a I/O operation. HTTP requests being executed by HttpClient can be aborted at any stage of execution by invoking HttpUriRequest#abort() method. This method is thread-safe and can be called from any thread. When an HTTP request is aborted its execution thread - even if currently blocked in an I/O operation - is guaranteed to unblock by throwing a InterruptedIOException

0
1

There is a cancel() method in AsyncTask class. Maintain a member to asynctask and cancel it in onDestroy(). then set the member to null.

Update

Use ClientConnectionManager to shutdown the connection.

http://developer.android.com/reference/org/apache/http/conn/ClientConnectionManager.html

Update 2

Check this link to set the timeout for your connection.

How to set HttpResponse timeout for Android in Java

4
  • The cancel method just puts a variable to true, does not actually close the AsyncTask, it is thought to loops that are demanding, and then in each iteration you check the value isCancelled(). This is not the case, such a loop does not exist, therefore I cannot check isCancelled()
    – jesbomar
    Sep 12, 2011 at 17:00
  • cancel() will attempt to cancel execution of the task. If you want the task to finish as soon as possible on cancelling, then you need to check the isCancelled and return if true. U misunderstood. developer.android.com/reference/android/os/…
    – Ron
    Sep 12, 2011 at 17:06
  • 1
    But look this: "A task can be cancelled at any time by invoking cancel(boolean). Invoking this method will cause subsequent calls to isCancelled() to return true. After invoking this method, onCancelled(Object), instead of onPostExecute(Object) will be invoked after doInBackground(Object[]) returns" We still need doInBackground to return, and that is the problem, it will never return because it is stucked in httpClient.execute(), am I wrong? :S
    – jesbomar
    Sep 12, 2011 at 17:12
  • You are right in your context. I updated my answer. you can set connection timeout.
    – Ron
    Sep 12, 2011 at 17:18
1

In onPause() or onBackButtonPressed(), call cancel() on your task. In doInBackground() right after

HttpResponse httpResponse = httpClient.execute(httpPost);

Check isCanceled() and immediatelly return if true.

Of course, you still have a risk of having multiple tasks running, but since this operation is UI driven (that is task started by user interactions) there should be at most couple of them running at the same time, provided that timeout on HttpClient is reasonable.


Update

You can shutdown connection manager as well once you determine that the task needs to be canceled. see docs

That should close sockets and cause immediate return from execute(). Connection manager is set when you create DefaultHttpClient.

3
  • How can i set that timeout, because it is not happening at the moment, I can be waiting for more than one or two minutes and no response is got.
    – jesbomar
    Sep 12, 2011 at 17:09
  • Actually you, probably should just shutdown connection manager. I added update. Sep 12, 2011 at 17:15
  • Sorry for the confussion, I just wrote a simpler code to avoid much code in the question, I use https and would like to mantain the connection manager so closing it would make me do the hand shaking again. I guess I will have to use timeouts.
    – jesbomar
    Sep 12, 2011 at 17:24
1

My understanding is that httpClient.execute() is blocking, so there is no code running to check the value of isCancelled(). And you'd prefer not to close the Connection Manager.

This might be a bit hacky, but in lieu of any better solutions, what happens if you call Thread.interrupt() on the Thread whilst httpClient.execute() is blocking?

A quick test can verify this, just add private instance variable of type Thread inside your ConnectionTask definition, set it to Thread.currentThread() at the top of doBackground(), and add a public method that calls .interrupt() on it.

If you're lucky that will cause httpClient.execute() to immediately exit, throwing an Exception. You can catch that and do any tidup you need to before the method call ends and the AsyncTask ends naturally.

4
  • Good idea! I am going to try that and come back with the results!
    – jesbomar
    Sep 12, 2011 at 17:38
  • It didnt bring any result... I try to do what you say, ` AsyncTask{ private Thread thisThread; @Override protected String doInBackground(String... params) { thisThread = Thread.currentThread(); [...] } public void cancelConnection(){ thisThread.interrupt(); } }` But there is no such a interruption... Changing from AsyncTask to Thread + Handler will help?
    – jesbomar
    Sep 13, 2011 at 15:50
  • Oh, so you're calling cancelConnection() from another thread and httpClient.execute() still just sits there? I'd feared that, that's why I said 'if you're lucky'... from the Android API docs it doesn't say that execute() throws InterruptedException, so it's not promised that it will. As it doesn't respect that then I can't see a way to stop it from just sitting there until it times out, sorry. Was worth a go though. No I don't think Handler will make any difference, execute() will still hang the Handler's Thread until it times out.
    – NeilS
    Sep 13, 2011 at 20:27
  • Thank you a lot for your help. I have been trying with no luck. I will have to implement a timeout for the .execute() as userSeven7s sais. Thank you again ;)
    – jesbomar
    Sep 13, 2011 at 23:20
0

You can override

onPause() method of the activity and write the stop connection code there. If the option is specific to back button then you can override

onBackButtonPressed() method to stop the execute.

1
  • Do you know how to stop the connection? Is it safe to access the httpClient from 2 different threads? I have tried, but there is not such method of cancel or abort connection... Do you have any idea?
    – jesbomar
    Sep 12, 2011 at 17:03
0

Well there is a quick solution for this problem which I used to solve in my case. It may not be the correct way, as when you press back the app will respond immediately but in background network operation will continue (until timeout if you set) without blocking the application:-

Do all you network operation in a new Service say NSERV. Make the NSERV extend the Thread class and do all you network operation in the run method. For more clarity in your code better make the activity/service starting the NSERV also extend Thread class and start the NSERV from their run method.

Then use static fields or singleton to access the variables in the activity/service from NSERV.

EX:

public class NSERV  extends Service implements Runnable{
public int onStartCommand (Intent intent, int flags, int startId)
{ 
        Thread t = new Thread (this, "abc");
        t.start();          
        return super.onStartCommand(intent, flags, startId);            
}

public void run() 
{
//NETWORK OPERATION...   
onDestroy();
}

public void onDestroy() 
{       
        stopSelf();
        super.onDestroy();
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.