问题描述
下面code基本按预期工作。然而,要偏执,我想知道,以避免资源泄漏,
- 请我需要调用
HttpURLConnection.disconnect
,完成其使用后? - 请我需要调用
InputStream.close
? - 请我需要调用
InputStreamReader.close
? - 我是否需要有以下的2线code:
httpUrlConnection.setDoInput(真)
和httpUrlConnection.setDoOutput(假)
,只是HttpURLConnection的? 的施工后
我想问这样的原因,是大多数我看到没有做这样清理的例子。 http://www.exampledepot.com/egs/java.net/post.html和http://www.vogella.com/articles/AndroidNetworking/article.html.我只是想确保这些例子是正确的为好。
公共静态字符串getResponseBodyAsString(字符串要求){
BufferedReader中的BufferedReader = NULL;
尝试 {
网址URL =新的URL(请求);
HttpURLConnection的HttpURLConnection的=(HttpURLConnection类)url.openConnection();
InputStream中的InputStream = httpUrlConnection.getInputStream();
的BufferedReader =新的BufferedReader(新的InputStreamReader(InputStream中));
INT charRead = 0;
的char []缓冲区=新的char [1024];
StringBuffer的StringBuffer的=新的StringBuffer();
而((charRead = bufferedReader.read(缓冲液))大于0){
的StringBuffer.append(缓冲液,0,charRead);
}
返回stringBuffer.toString();
}赶上(MalformedURLException异常E){
Log.e(TAG,,E);
}赶上(IOException异常E){
Log.e(TAG,,E);
} 最后 {
关闭(BufferedReader类);
}
返回null;
}
私有静态无效关闭(阅读器阅读器){
如果(读者!= NULL){
尝试 {
reader.close();
}赶上(IOException异常EXP){
Log.e(TAG,,EXP);
}
}
}
是的,你需要首先关闭InputStream和关闭HttpConnection的下一步。按照的javadoc 。
接下来的两个问题的答案取决于你的连接的目的。阅读link 的更多细节。
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?
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.html and http://www.vogella.com/articles/AndroidNetworking/article.html. I just want to make sure those examples are correct as well.
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);
}
}
}
Yes you need to close the inputstream first and close httpconnection next. As per javadoc.
Next two questions answer depends on purpose of your connection. Read this link for more details.
这篇关于我是否需要使用它完成后调用HttpURLConnection.disconnect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!