I'm using AsyncTask
to initalize AndroidHttpClient
and execute a POST request in doInBackground()
. I'd like the user to be able to cancel the request by pressing the back button. AsyncTask
has a cancel()
method which only changes the boolean return value of isCancelled()
and then waits for doInBackground()
to finish before calling onCancelled()
. This means that AsyncTask
leaves it up to the doInBackground()
method to continuously check whether the task has been cancelled (using isCancelled()
). If it has been cancelled, doInBackground()
should return prematurely prematurely. The problem I'm having is that 99% the execution of the worker thread in doInBackground()
stops on:
HttpResponse response = httpClient.execute(request[0]);
because this synchronous function call encapsulates the network aspects of the request. How can I cancel the request midway through?
I'm considering trying to change the timeout time during the request, but this seems thread unsafe.
I'm also considering overriding AsyncTask
's cancel()
method, which may work better.
Any suggestions?