我有一个循环的一堆URL,对于每一个我正在做以下:
private String doQuery(String url) {
HttpGet httpGet = new HttpGet(url);
setDefaultHeaders(httpGet); // static method
HttpResponse response = httpClient.execute(httpGet); // httpClient instantiated in constructor
int rc = response.getStatusLine().getStatusCode();
if (rc != 200) {
// some stuff...
return;
}
HttpEntity entity = response.getEntity();
if (entity == null) {
// some stuff...
return;
}
// process the entity, get input stream etc
}
第一个查询是好的,第二个抛出这个异常:
Exception in thread “main”
java.lang.IllegalStateException:
Invalid use of
SingleClientConnManager: connection
still allocated. Make sure to release
the connection before allocating
another one. at
org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:199)
at
org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:173)……
这只是一个简单的单线程应用程序。如何释放此连接?