Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusing HttpURLConnection

In my app i'm using code like the following to download several images. Is it High-Performance to do it like that or can I reuse the connection somehow?

   for(int i = 0; i < 100; i++){  
    URL url = new URL("http://www.android.com/image" + i + ".jpg");
           HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
           try {
             InputStream in = new BufferedInputStream(urlConnection.getInputStream());
             readStream(in);
            finally {
             urlConnection.disconnect();
           }
         }
}
like image 394
user1697423 Avatar asked Nov 23 '12 10:11

user1697423


People also ask

What is the difference between URLConnection and HttpURLConnection?

URLConnection is the base class. HttpURLConnection is a derived class which you can use when you need the extra API and you are dealing with HTTP or HTTPS only. HttpsURLConnection is a 'more derived' class which you can use when you need the 'more extra' API and you are dealing with HTTPS only.

Is HttpURLConnection thread safe?

Each instance of HttpURLConnection may be used for one request/response pair. Instances of this class are not thread safe.

How do I close HttpURLConnection?

To close the connection, invoke the close() method on either the InputStream or OutputStream object. Doing that may free the network resources associated with the URLConnection instance.

What is the use of HttpURLConnection?

By the help of HttpURLConnection class, you can retrieve information of any HTTP URL such as header information, status code, response code etc. The java. net. HttpURLConnection is subclass of URLConnection class.


1 Answers

You won't really get any benefit from reuse of the HttpURLConnection.

One thing that will greatly benefit your application is if you spend some time looking into Async Tasks, which will allow you to harness the power of multi threaded HTTP requests with callbacks to your main code.

See: http://www.vogella.com/articles/AndroidPerformance/article.html for a good example of how Async Tasks can be utilised.

A good starting point though is of course the Android Developers Blog, where they have an example for downloading an image from a server asynchronously, which will match your requirements nicely. With some adaptation you can have your application sending multiple asynchronous requests at once for good performance.

The Google article can be found at: http://android-developers.blogspot.co.uk/2009/05/painless-threading.html

The key area to look at is:

public void onClick(View v) {
  new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask {
     protected Bitmap doInBackground(String... urls) {
         return loadImageFromNetwork(urls[0]);
     }

     protected void onPostExecute(Bitmap result) {
         mImageView.setImageBitmap(result);
     }
 }

The loadImageFromNetwork method is where the downloading takes place, and will be completely asynchronous away from your main UI thread.

As a basic example, you could modify your application to call this like so:

for(int i = 0; i < 100; i++){  
    new DownloadImageTask().execute("http://www.android.com/image" + i + ".jpg");
}

Though for an optimisation, I wouldn't throw 100 requests out at once, maybe creating a Threaded queue system which will allow maybe 4 or 5 concurrent connections and then keep the rest coming through when another finishes by maintaining an ArrayList of pending requests to read off.

like image 144
biddulph.r Avatar answered Oct 06 '22 17:10

biddulph.r