mun*_*ikh 4 java android inputstream bytearrayoutputstream android-strictmode
严格模式会引发以下情况:在附加的堆栈跟踪中获取资源但从未释放.有关避免资源泄漏的信息,请参阅java.io.Closeable:
**response = httpclient.execute(httpPost);**
Run Code Online (Sandbox Code Playgroud)
以下是我的代码:
HttpClient httpclient = new DefaultHttpClient();
String url = "example";
HttpPost httpPost = new HttpPost(url);
HttpResponse response;
String responseString = "";
try {
httpPost.setHeader("Content-Type", "application/json");
**response = httpclient.execute(httpPost);**
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return responseString;
Run Code Online (Sandbox Code Playgroud)
提前致谢.
从4.3开始,kenota指出的方法已被弃用.
而不是HttpClient
你现在应该使用CloseableHttpClient
如下所示:
CloseableHttpClient client= HttpClientBuilder.create().build();
Run Code Online (Sandbox Code Playgroud)
然后你可以用以下方法关闭它:
client.close();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6410 次 |
最近记录: |