如何使用ThreadSafeClientConnManager.requestConnection(HttpRoute route,Object state)

时间:2010-09-11 01:54:06

标签: android apache

使用ThreadSafeClientConnManager.requestConnection(HttpRoute route,Object state),第二项“state”应该是什么?

最终我正在尝试ThreadSafeClientConnManager.releaseConnection(ManagedClientConnection conn,long validDuration,TimeUnit timeUnit)来释放连接。但我需要ManagedClientConnection,它只能从requestConnection方法获得(通过返回的ClientConnectionRequest)

2 个答案:

答案 0 :(得分:2)

看起来您应该能够安全地为状态参数传递null。

DefaultRequestDirector调用requestConnection,就像这样...

Object userToken = context.getAttribute(ClientContext.USER_TOKEN);

// Allocate connection if needed
if (managedConn == null) {
    ClientConnectionRequest connRequest = connManager.requestConnection(route, userToken);

进一步向上链接,如果context为null(就像调用client.execute(httpRequest)时那样),则上下文被默认上下文替换,该上下文不包含USER_TOKEN,因此调用requestConnection为null州参数。

答案 1 :(得分:1)

我找到了一个关于如何使用这个类的相当不错的例子(参见底部的链接)。如果您对回复执行releaseConnection(),则不需要entity.consumeContent()。还要确保将BasicHttpContext传入httpclient.execute。

与此类似:

this.context = new BasicHttpContext();

httpclient.execute(httppost,context);
String entityStr = EntityUtils.toString(response.getEntity());
response.getEntity().consumeContent();

http://svn.apache.org/repos/asf/httpcomponents/httpclient/branches/4.0.x/httpclient/src/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java

仍然不知道requestConnection中的“状态”是什么:ThreadSafeClientConnManager.requestConnection(HttpRoute route, Object state)