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;
}
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.