2

I am using a static web services class to call an HTTP GET request and want to be able to cancel the request/AsyncTask when the user presses the back button.

I check isCancelled() periodically in doInBackground, which works for all other operations, but if the HTTP request is taking place when the user hits back then the task won't be canceled until the result is received or it times out.

Because of the static call to WebServices.getDeviceId() I do not have a handle on the HTTP client, so I can't use httpclient.getConnectionManager().shutdown(); to stop the connection. Anybody have any ideas on how I can solve this issue?

@Override
    protected Void doInBackground(Void... params) {
        if((mInstance.getDeviceId() == null) && !isCancelled()) {
            String deviceId = WebServices.getDeviceId();
        }

        return null;
    }
AMIC MING
  • 6,306
  • 6
  • 46
  • 62
starkej2
  • 11,391
  • 6
  • 35
  • 41
  • You write `I check isCancelled() periodically` but I think you are wrong: you are checking `isCancelled()` only once, at the start of `doInBackground()`. – vault Jan 14 '13 at 14:31
  • Well right now this is the only thing that is done in doInBackground, so I only check it once since there are no loops or anything and I can't check it during the actual HTTP stuff – starkej2 Jan 14 '13 at 14:48
  • Yes you are right, I just wanted to check what you were meaning with "periodically", because there are no loops. – vault Jan 14 '13 at 15:31

2 Answers2

7

If you want to interrupt the http request, you need a reference to it and call yourHttpRequest.abort()

In WebServices class, can't you add a method like abortHttpRequest() and add a reference to the current HttpRequest?

You may need to manage a queue of http requests and abort the correct one.

vault
  • 3,930
  • 1
  • 35
  • 46
  • I thought about that, but since the methods are accessed statically, how would I know which HttpRequest was current? – starkej2 Jan 14 '13 at 14:30
  • If you go this way you need to manage a queue of HttpRequests, [read this](http://stackoverflow.com/questions/6368815/asynchronous-http-request-queuing-requests). You probably won't need AsyncTasks anymore and you will have references to the queued request. – vault Jan 14 '13 at 15:35
1

This is from SDK:

 Cancelling a task

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. 
To ensure that a task is cancelled as quickly as possible, 
you should always check the return value of isCancelled() periodically from 
doInBackground(Object[]), if possible (inside a loop for instance.)

and then

if (isCancelled()) 
     break;
else
{
   // do your work here
}

this is the answer on so

Community
  • 1
  • 1
v0d1ch
  • 2,738
  • 1
  • 22
  • 27
  • 2
    Yeah, I do that already to cancel the AsyncTask, but if cancel is called while the HTTP request is running, the task won't get canceled until the request either finishes or times out – starkej2 Jan 14 '13 at 14:24
  • 2
    Canceling the task does not interrupt http request – vault Jan 14 '13 at 14:24
  • can you do httpclient.getConnectionManager().shutdown(); in onCanccelled() – v0d1ch Jan 14 '13 at 14:38