在Android上应该优先使用HttpURLConnection而不是HttpClient。
conn.connect()
这句不需要显式调用,后面的方法会自动调用,但是显式调用了也不会怀孕。
disconnect()
在其他的JVM实现中会关闭闲置的连接。所以一般不显示调用,但是Android的实现不同,需要调用
Once the response body has been read, the HttpURLConnection should be closed by calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed or reused.
Android上早起版本有一个bug,所以在早期版本要禁用连接池。
private void disableConnectionReuseIfNecessary() {
// HTTP connection reuse which was buggy pre-froyo
if (Integer.parseInt(Build.VERSION.SDK) < Build.VERSION_CODES.FROYO) {
System.setProperty("http.keepAlive", "false");
}
}
时间: 2024-11-09 01:26:44