我正在使用HttpURLConnection下载文件.我可以从另一个线程取消下载吗?如果没有,应该使用什么文件下载方法?
而不是HttpUrlConnection.这是一个更好的图书馆在许多方面.
然后,您可以使用:
final SimpleHttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); final HttpClient client = new HttpClient(connectionManager); // ... and when it's time to close the connection from another thread connectionManager.shutdown();
如果您需要跨多个线程关闭多个连接,则可以重复使用一个HttpClient实例,并以类似的方式将它们全部关闭:
static MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager(); // HttpClient instance that can be shared across threads and create multiple connections static HttpClient client = new HttpClient(connectionManager); // ... and when it's time to close connections connectionManager.shutdown();
代码日志版权声明:
翻译自:http://stackoverflow.com/questions/4442424/cancel-file-download-from-another-thread-at-any-time