java.net.UnknownHostException:如何在异常捕获

时间:2017-02-20 16:05:31

标签: java android networking error-handling retrypolicy

受人尊敬的大师 我是android和JAVA世界的新手。 尝试下载一系列图像时出现以下错误。 “URL /主机名正确的位置” 错误是:

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)

“有些时候相同的代码效果很好,但大多数情况下它会遇到上述错误并且应用程序在Play商店中获得评级”

导致上述错误的代码是

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;
    }
}

我想在

上一次又一次地重试网址
  

“UnknownHostException Catch”

直到图像正确加载。 您的建议将不胜感激。 或者在UnknownHostException上一次又一次地加载特定URL的正确和精确方法是什么?

0 个答案:

没有答案