开发者

Is there a way to stop a HttpClient's execution in an AsyncTask?

I have an AsyncTask that handles a rather long download, using HttpClient. I'd like to stop the execution of this request if the user finishes the Activity that start开发者_JAVA技巧s it. How can I achieve this?

So I started the AsyncTask, HttpClient is in execute(), processing a GET request, is there a way to terminate this?


  1. Call AsyncTask#cancel to discard pending task
  2. according to HttpClient docs, use HttpUriRequest#abort() to abort http 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


Async task has a cancel call that you could override. You could get at your HttpClient from there and close the stream.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜