处理 AsyncTask 中的 "Server not responding" 异常

Handle "Server not responding" exception in AsyncTask

AsyncTask 由于某种原因服务器 "down" 无法连接到服务器时,您如何处理 RuntimeException

我尝试的是一堆 catch 块(没有帮助):

        try {
            // Create Request to server and get response
            HttpGet httpget = new HttpGet(activity.getString(R.string.get_channels_lang_host));
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            downloadedString = httpclient.execute(httpget, responseHandler);
        }catch (MalformedURLException e) {
            Log.e("URL:","is a malformed URL");
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            Log.e("URL:"," UnsupportedEncodingException");
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            Log.e("URL:"," ClientProtocolException");
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
            Log.e("URL:"," SocketTimeoutException");
        } catch (ConnectTimeoutException e) {
            e.printStackTrace();
            Log.e("URL:"," ConnectTimeoutException");
        } catch (IOException e) {
            Log.e("URL:","IOException");
            e.printStackTrace();
        }
        if (downloadedString != null) {
            //parse request to object
            dataChannelsLangArrayList = JsonParser.getDataChannelsLang(downloadedString);
        } else {
            Toast.makeText(ActivityLoading.this, "Failed to load data. Restarting...", Toast.LENGTH_LONG).show();
            Global.restartApp(ActivityLoading.this);

        }

我仍然遇到运行时错误:

04-09 11:31:29.610    3696-3719/tenkol.design.com.imbrecords E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:864)
 Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
        at android.os.Handler.<init>(Handler.java:121)
        at android.widget.Toast$TN.<init>(Toast.java:322)
        at android.widget.Toast.<init>(Toast.java:91)
        at android.widget.Toast.makeText(Toast.java:238)
        at tenkol.design.com.imbrecords.Global.restartApp(Global.java:115)
        at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.getChannelsLang(ActivityLoading.java:249)
        at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.doInBackground(ActivityLoading.java:157)
        at tenkol.design.com.imbrecords.ActivityLoading$LoadingAsyncTask.doInBackground(ActivityLoading.java:141)
        at android.os.AsyncTask.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:864)

我最感兴趣的有两种情况:

  1. 当服务器完全没有响应时;
  2. 当请求花费的时间太多时(即网速慢)。

处理这些问题的正确方法是什么?

Can't create handler inside thread that has not called Looper.prepare()

好吧,您已经创建了一个 Handler,但是它链接到的线程没有任何消息队列 post 上的东西。

尝试 new Handler(Looper.getMainLooper()) 链接到主线程的处理程序。

然后你可以post一个Runnable在它上面显示敬酒。

但说真的,为什么不收集异常和结果并在 onPostExecute() 中全部处理?

Public class Result<T>{
  public T data;
  public Exception exception;
  public String message;
}