2

Respectable gurus I am new in the android and JAVA universe. I am getting the following error while try to download a series of images. "Where as The URL / hostname is correct" The Error is:

java.net.UnknownHostException: Unable to resolve host "lh3.googleusercontent.com": No address associated with hostname
at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.ht...(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
at com.myappid.ImageLoader1.getBitmap(ImageLoader1.java:73)
at com.myappid.ImageLoader1.access$000(ImageLoader1.java:24)
at com.myappid.ImageLoader1$PhotosLoader.run(ImageLoader1.java:149)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
at java.util.concurrent.FutureTas...(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)

"Some time Same code works great but most of the time it stuck with the above error and app get down rating on play store"

The Code causing the above error is

private Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    Bitmap b = decodeFile(f);
    if (b != null)
        return b;

    // Download Images from the Internet
    try {
        Bitmap bitmap = null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        conn.setInstanceFollowRedirects(true);
        InputStream is = conn.getInputStream();
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        conn.disconnect();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Throwable ex) {
        ex.printStackTrace();
        if (ex instanceof OutOfMemoryError) {

            clearCache();
        }
        if (ex instanceof UnknownHostException) {
            /* What Should Be there to execute same url again and again Until it loads correctly??
       I use This method 
       getBitmap(url)
       But its not working in expected manner Please give me a hint */
        }
        return null;
    }
}

What I want to retry the url again and again on

"UnknownHostException Catch"

until the image loads correctly. Your suggestion will be appreciated. Or what will be the correct and fine way to load specific url again and again on UnknownHostException?

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • 1
    Possible duplicate of [Design pattern for "retrying" logic that failed?](http://stackoverflow.com/questions/11692595/design-pattern-for-retrying-logic-that-failed) – Fusselchen Feb 20 '17 at 16:10

0 Answers0