Android 使用完后是否需要调用 HttpURLConnection.disconnect
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11056088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to call HttpURLConnection.disconnect after finish using it
提问by Cheok Yan Cheng
The following code basically works as expected. However, to be paranoid, I was wondering, to avoid resource leakage,
以下代码基本上按预期工作。然而,偏执,我想知道,为了避免资源泄漏,
- Do I need to call
HttpURLConnection.disconnect
, after finish its usage? - Do I need to call
InputStream.close
? - Do I need to call
InputStreamReader.close
? - Do I need to have the following 2 line of code :
httpUrlConnection.setDoInput(true)
andhttpUrlConnection.setDoOutput(false)
, just after the construction of httpUrlConnection?
HttpURLConnection.disconnect
完成使用后是否需要调用, ?- 我需要打电话
InputStream.close
吗? - 我需要打电话
InputStreamReader.close
吗? - 我是否需要在构建 httpUrlConnection 之后使用以下 2 行代码:
httpUrlConnection.setDoInput(true)
和httpUrlConnection.setDoOutput(false)
?
The reason I ask so, is most of the examples I saw do not do such cleanup. http://www.exampledepot.com/egs/java.net/post.htmland http://www.vogella.com/articles/AndroidNetworking/article.html. I just want to make sure those examples are correct as well.
我这么问的原因是,我看到的大多数示例都没有进行此类清理。http://www.exampledepot.com/egs/java.net/post.html和http://www.vogella.com/articles/AndroidNetworking/article.html。我只是想确保这些例子也是正确的。
public static String getResponseBodyAsString(String request) {
BufferedReader bufferedReader = null;
try {
URL url = new URL(request);
HttpURLConnection httpUrlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = httpUrlConnection.getInputStream();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
int charRead = 0;
char[] buffer = new char[1024];
StringBuffer stringBuffer = new StringBuffer();
while ((charRead = bufferedReader.read(buffer)) > 0) {
stringBuffer.append(buffer, 0, charRead);
}
return stringBuffer.toString();
} catch (MalformedURLException e) {
Log.e(TAG, "", e);
} catch (IOException e) {
Log.e(TAG, "", e);
} finally {
close(bufferedReader);
}
return null;
}
private static void close(Reader reader) {
if (reader != null) {
try {
reader.close();
} catch (IOException exp) {
Log.e(TAG, "", exp);
}
}
}
采纳答案by kosa
Yes you need to close the inputstream first and close httpconnection next. As per javadoc.
是的,您需要先关闭输入流,然后再关闭 httpconnection。根据javadoc。
Each HttpURLConnection instance is used to make a single request but the underlying network connection to the HTTP server may be transparently shared by other instances. Calling the close() methods on the InputStream or OutputStream of an HttpURLConnection after a request may free network resources associated with this instance but has no effect on any shared persistent connection. Calling the disconnect() method may close the underlying socket if a persistent connection is otherwise idle at that time.
每个 HttpURLConnection 实例用于发出单个请求,但与 HTTP 服务器的底层网络连接可能由其他实例透明地共享。在请求之后调用 HttpURLConnection 的 InputStream 或 OutputStream 上的 close() 方法可能会释放与此实例关联的网络资源,但对任何共享的持久连接没有影响。如果持久连接当时处于空闲状态,则调用 disconnect() 方法可能会关闭底层套接字。
Next two questions answer depends on purpose of your connection. Read this link for more details.
接下来的两个问题的答案取决于您的连接目的。阅读此链接了解更多详情。
回答by Dennis
I believe the requirement for calling setDoInput() or setDoOutput() is to make sure they are called before anything is written to or read from a stream on the connection. Beyond that, I'm not sure it matters when those methods are called.
我相信调用 setDoInput() 或 setDoOutput() 的要求是确保在连接上的流中写入或读取任何内容之前调用它们。除此之外,我不确定何时调用这些方法是否重要。