Yes, you should. According to the JavaDoc: http://docs.oracle.com/javase/7/docs/api/java/net/HttpURLConnection.html
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. 2014-04-17 16:58 GMT+02:00 yccheok <[email protected]>: > Google is providing 2 different examples of HttpURLConnection usage. > Not calling HttpURLConnection's disconnect > > http://developer.android.com/training/basics/network-ops/connecting.html > > // 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(); > } > }} > > ------------------------------ > Calling HttpURLConnection's disconnect > > http://developer.android.com/reference/java/net/HttpURLConnection.html > > 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(); > } > } > > ------------------------------ > > This rather confuses us as developer. Should we, call HttpURLConnection's > disconnect or not? > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > --- > You received this message because you are subscribed to the Google Groups > "Android Developers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- Enrique López Mañas *Twitter:* http://twitter.com/eenriquelopez *Google+:* http://goo.gl/1PLi2a *Phone:* +34 655 155 199 (ES) +49 157 84281461 (DE) *Site:* http://www.lopez-manas.com -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en --- You received this message because you are subscribed to the Google Groups "Android Developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

