执行此代码时,我将收到以下错误消息。
一个资源是在附加的堆栈跟踪中获得的,但从未释放过。有关避免资源泄漏的信息,请参见java.io.Closeable .:
我无法识别以下代码中的资源泄漏。如果有人指出我到底做错了什么,我会很高兴的。
HttpPost request = new HttpPost(url);
StringBuilder sb = new StringBuilder();
StringEntity entity = new StringEntity(jsonString);
entity.setContentType("application/json;charset=UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
request.setHeader("Accept", "application/json");
request.setEntity(entity);
HttpResponse response = null;
DefaultHttpClient httpclient = getHttpClientImpl();
BufferedReader reader = null;
InputStream in = null;
try {
response = httpclient.execute(request);
in = response.getEntity().getContent();
reader = new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception se) {
Log.e("Exception", se + "");
throw se;
} finally {
if (in != null)
in.close();
if (reader != null)
reader.close();
if (client != null && client.getConnectionManager() != null) {
client.getConnectionManager().shutdown();
}
}
return sb.toString();发布于 2012-12-10 11:00:37
我并不认为该代码存在任何问题,但我建议关闭池中的所有空闲连接。
public abstract void closeIdleConnections (long idletime, TimeUnit tunit)此外,当DefaultHttpClient没有完全正确配置时,也存在一些已知的问题。我建议使用AndroidHttpClient作为HttpClient接口的实现。请访问下列文件以获得解释:
http://developer.android.com/reference/android/net/http/AndroidHttpClient.html
https://stackoverflow.com/questions/13799411
复制相似问题