HttpURLConnection catch block repeating

597 views Asked by At

I am testing incomplete URLs in AsyncTask to handle missing image and using default drawable for my imageview. I noticed that the stacktrace is printed several times. I worry this is also creating that bitmap over and over again. Is this normal? Any suggestions for handling this better?

Code:

@Override
protected Bitmap doInBackground(String... params) {
    Bitmap artistImg = null;
    HttpURLConnection urlConnection = null;

    try {
        URL url = new URL(params[0]);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.connect();

        InputStream is = urlConnection.getInputStream();
        artistImg = BitmapFactory.decodeStream(is);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        artistImg = BitmapFactory.decodeResource(
               context.getResources(), R.drawable.question);

    } catch (Exception e) {
        Log.v(LOG_TAG, "NETWORK ERROR");
    } finally {
        if (urlConnection != null) urlConnection.disconnect();
    }

    return artistImg;
}

Stacktrace (repeated 15+ times):

06-24 10:09:49.354  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ java.io.FileNotFoundException: https://i.scdn.co/image/792a417e8500405
06-24 10:09:49.354  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:206)
06-24 10:09:49.355  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.android.okhttp.internal.http.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
06-24 10:09:49.355  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25)
06-24 10:09:49.355  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.zosocoder.android.spotifystreamer.ArtistAdapter$FetchArtistThumbTask.doInBackground(ArtistAdapter.java:85)
06-24 10:09:49.355  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at com.zosocoder.android.spotifystreamer.ArtistAdapter$FetchArtistThumbTask.doInBackground(ArtistAdapter.java:68)
06-24 10:09:49.355  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292)
06-24 10:09:49.355  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-24 10:09:49.356  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
06-24 10:09:49.356  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-24 10:09:49.356  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-24 10:09:49.356  21523-21567/com.zosocoder.android.spotifystreamer W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
2

There are 2 answers

1
user2641570 On BEST ANSWER

You might consider using either Picasso or Glide to load your images. It would make your image loading much simpler. Both of those libraries handle loading images in the background/caching already loaded images on disk/specifying default images in case your request fails while retrieving the image.

Loading an image would simply be:

Glide.with(context)
    .load("http://goo.gl/gEgYUd")
    .placeholder(R.drawable.loading_spinner)
    .into(imageView);

I personally prefer Glide since I find it to be faster but both of those libraries work well and would spare you the trouble of handling your bitmaps manually.

1
Rahul M Mohan On

Android Query

Android-Query (AQuery) is a light-weight library for doing asynchronous tasks and manipulating UI elements in Android. you can get aquery from here

https://code.google.com/p/android-query/downloads/detail?name=android-query-full.0.24.3.jar

And documentaton from

https://code.google.com/p/android-query/wiki/ImageLoading

you can load images from url to your imageview by the following simple code

AQuery aq = new AQuery(getApplicationContext());
ImageView image=(ImageView)findViewById(R.id.imageview);
aq.id(image).image("http://www.vikispot.com/z/images/vikispot/android-w.png");