我是否需要使用它完成后调用HttpURLConnection.disconnect [英] Do I need to call HttpURLConnection.disconnect after finish using it

查看:339
本文介绍了我是否需要使用它完成后调用HttpURLConnection.disconnect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code基本按预期工作。然而,要偏执,我想知道,以避免资源泄漏,

  1. 请我需要调用 HttpURLConnection.disconnect ,完成其使用后?
  2. 请我需要调用 InputStream.close
  3. 请我需要调用 InputStreamReader.close
  4. 我是否需要有以下的2线code: httpUrlConnection.setDoInput(真) httpUrlConnection.setDoOutput(假) ,只是HttpURLConnection的?
  5. 的施工后

我想问这样的原因,是大多数我看到没有做这样清理的例子。 <一href="http://www.exampledepot.com/egs/java.net/post.html">http://www.exampledepot.com/egs/java.net/post.html和<一href="http://www.vogella.com/articles/AndroidNetworking/article.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

  

每个HttpURLConnection实例是用来制造单个请求,但基础网络连接到HTTP服务器可以由其它实例可以透明共用。调用上的HttpURLConnection的InputStream的或OutputStream的关闭()方法的请求后,可能释放与此实例相关的网络资源,但对任何共享的持久连接没有任何影响。调用disconnect()方法可以关闭底层插座是否持久连接处于空闲状态,在那个时候。

接下来的两个问题的答案取决于你的连接的目的。阅读<一个href="http://developer.android.com/reference/java/net/URLConnection.html#setDoInput%28boolean%29">link 的更多细节。

The following code basically works as expected. However, to be paranoid, I was wondering, to avoid resource leakage,

  1. Do I need to call HttpURLConnection.disconnect, after finish its usage?
  2. Do I need to call InputStream.close?
  3. Do I need to call InputStreamReader.close?
  4. Do I need to have the following 2 line of code : httpUrlConnection.setDoInput(true) and httpUrlConnection.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.

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.

Next two questions answer depends on purpose of your connection. Read this link for more details.

这篇关于我是否需要使用它完成后调用HttpURLConnection.disconnect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆