title: Google provides 2 different examples of HttpURLConnection usage - Should we call HttpURLConnection's disconnect, or InputStream's close?
body: <p>Google is providing 2 different examples of <code>HttpURLConnection</code> usage.</p>
  
  <h1>Calling <code>InputStream</code>'s <code>close</code></h1>
  
  <p><a href="http://developer.android.com/training/basics/network-ops/connecting.html" rel="noreferrer">http://developer.android.com/training/basics/network-ops/connecting.html</a></p>
  
  <pre><code>// Given a URL, establishes an HttpUrlConnection and retrieves
  // the web page content as a InputStream, which it returns as
  // a string.
  private String downloadUrl(String myurl) throws IOException {
      InputStream is = null;
      // Only display the first 500 characters of the retrieved
      // web page content.
      int len = 500;
  
      try {
          URL url = new URL(myurl);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setReadTimeout(10000 /* milliseconds */);
          conn.setConnectTimeout(15000 /* milliseconds */);
          conn.setRequestMethod("GET");
          conn.setDoInput(true);
          // Starts the query
          conn.connect();
          int response = conn.getResponseCode();
          Log.d(DEBUG_TAG, "The response is: " + response);
          is = conn.getInputStream();
  
          // Convert the InputStream into a string
          String contentAsString = readIt(is, len);
          return contentAsString;
  
      // Makes sure that the InputStream is closed after the app is
      // finished using it.
      } finally {
          if (is != null) {
              is.close();
          } 
      }
  }
  </code></pre>
  
  <hr>
  
  <h1>Calling <code>HttpURLConnection</code>'s <code>disconnect</code></h1>
  
  <p><a href="http://developer.android.com/reference/java/net/HttpURLConnection.html" rel="noreferrer">http://developer.android.com/reference/java/net/HttpURLConnection.html</a></p>
  
  <pre><code>   URL url = new URL("http://www.android.com/");
     HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
     try {
       InputStream in = new BufferedInputStream(urlConnection.getInputStream());
       readStream(in);
      finally {
       urlConnection.disconnect();
     }
   }
  </code></pre>
  
  <hr>
  
  <p>For <strong>resource leakage</strong> and <strong>performance consideration</strong> (Need not to setup network connection from ground up, as my app will communicate with same server most of the time), should we </p>
  
  <ol>
  <li>Call <code>HttpURLConnection</code>'s <code>disconnect</code> only.</li>
  <li>Call the <code>InputStream</code>'s <code>close</code> only.</li>
  <li>Call both <code>HttpURLConnection</code>'s <code>disconnect</code> &amp; <code>InputStream</code>'s <code>close</code> (Haven't seen such official example so far).</li>
  </ol>