Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从ThreadSafeClientConnManager连接池取消/中止连接_Java_Android_Thread Safety_Connection Pooling_Httpclient - Fatal编程技术网

Java 从ThreadSafeClientConnManager连接池取消/中止连接

Java 从ThreadSafeClientConnManager连接池取消/中止连接,java,android,thread-safety,connection-pooling,httpclient,Java,Android,Thread Safety,Connection Pooling,Httpclient,我使用来管理客户端连接池,因为我的应用程序有几个线程,它们同时连接到Web服务器 摘要示例代码: HttpClient httpClient; ClientConnectionManager conMgr = new ThreadSafeClientConnManager(parameters,schReg); httpclient = new DefaultHttpClient(conMgr, parameters); 现在让我们假设其中一个线程正在下载一个大文件,但是我的应用程序的用户正在

我使用来管理客户端连接池,因为我的应用程序有几个线程,它们同时连接到Web服务器

摘要示例代码:

HttpClient httpClient;
ClientConnectionManager conMgr = new ThreadSafeClientConnManager(parameters,schReg);
httpclient = new DefaultHttpClient(conMgr, parameters);
现在让我们假设其中一个线程正在下载一个大文件,但是我的应用程序的用户正在切换到另一个活动/屏幕。因此,该文件是不必要的,我想中止这个下载连接

在我的研究中,我发现了这种方法:

publicclientconnectionrequestconnection(HttpRoute路由,对象状态)
返回新的ClientConnectionRequest,从中可以获得ManagedClient连接或中止请求

到目前为止,我一直在使用:

HttpGet httpRequest = new HttpGet(URL_TO_FILE);
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
[...]
现在根据我的理解,我必须使用:

httpclient.getConnectionManager().requestConnection(HttpRoute路由,对象状态)

这就是我被困的地方。我假设对于路由,我可以只使用
newhttproute(newhttphost(“10.0.0.1”))
或我的服务器是什么,但是要为
对象状态输入什么

第二,只要我有了
ClientConnectionManager
,我就可以调用
getConnection(长超时,时间单位tunit)
。然后,我如何执行我的
HttpGet-httpRequest=newhttpget(URL\u-TO\u文件)就像我之前对
HttpResponse response=(HttpResponse)httpclient.execute(httpRequest)所做的那样


我已经阅读了文档并尝试了很多不同的东西,但我无法获得一个有效的解决方案。因此,任何建议和/或代码示例都是非常受欢迎的。

您只需调用
httpRequest.abort()
,连接就应该关闭

对于大文件,必须有一个循环来处理数据。您只需检查取消状态并从此处中止

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        while (instream.read(buf) >= 0) {
            if (cancelled)
               httpRequest.abort();
            // Process the file
        }
     }

当使用pooling或keepalive时,中止的连接无法返回到池,必须将其关闭。在旧版本中存在一个bug,即连接保持活动状态,这会打乱下一个请求。我想这一切都解决了。

谢谢。这比我想做的容易多了;-)中止()后,该连接不再显示在netstat中。我想那时它已经关闭了。