AnZ
AnZ

Reputation: 1060

Handle "Server not responding" exception in AsyncTask

How do you handle the RuntimeException when AsyncTask couldn't connect to server since server is "down" for some reason?

What do I try is a bunch of catch blocks (doesn't help):

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

        }

i'm still getting the runtime error:

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$3.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$1.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$2.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$1.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)

I'm mostly interested in two cases:

  1. When server doesn't respond at all;
  2. When the time spend for request is too big (i.e. with slow Internet).

What is the proper way to handle those?

Upvotes: 0

Views: 1533

Answers (1)

s.d
s.d

Reputation: 29436

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

Well, you have created a Handler, but the thread it is linked to doesn't have any message queue to post stuff on.

Try new Handler(Looper.getMainLooper()) for a handler linked to main thread.

Then you can post a Runnable on it which shows a toast.

But seriously, why not collect exception and result both and handle it all in onPostExecute() ?

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

Upvotes: 3

Related Questions