Android: AsyncTask timeout

android, android-asynctask, timeout

Solution

because you are trying to start AsyncTask again inside `doInBackground` when it's still running . change your code as to get it work :

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash_layout);
    downloadFAQ = new DownloadFAQ();
    new Thread(new Runnable() {
            public void run() {
                try {
                    downloadFAQ.execute().get(2000, TimeUnit.MILLISECONDS);

                  SplashActivity.thisrunOnUiThread(new Runnable() {
                        public void run() {
                         // start Activity here
                          Intent i = new Intent(SplashActivity.this,
                                                      TabsActivity.class);
                          SplashActivity.this.startActivity(i);
                          SplashActivity.this.finish();
                       }
                  });
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (TimeoutException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).start();
}

and you will need to remove `downloadFAQ.get(2000, TimeUnit.MILLISECONDS);` from doInBackground method change your AsyncTask as

private class DownloadFAQ extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        ServerAPI server = new ServerAPI(getApplicationContext());
        server.serverRequest(ServerAPI.GET_FAQ, null);
        return null;
    }

    protected void onPostExecute(Void result) {

    }

}

Problem

Is it possible to do splash screen that will execute HTTP request and if this request is executing too long, i.e. 7-10 seconds, then abort the request and jump to the main activity? The below code is what I did, but it doesn't work - the timeout isn't working, the HTTP request and jumping are working. As I understand, it's possible to use the `AsyncTask`'s `get()` method or handler with delay. `Get()` method should be in separate thread but it doesn't work. How to do this task? EDIT: ``` public class SplashActivity extends Activity { private static final String TAG = "SplashActivity"; private Handler handler = new Handler(); private Runnable r; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.splash_layout); if (Helpers.isNetworkConnected(getApplicationContext())) { Log.d(TAG, "Has Internet"); final DownloadFAQ downloadFAQ = new DownloadFAQ(); new Thread(new Runnable() { public void run() { try { Log.d(TAG, "Timing..."); downloadFAQ.execute().get(1000, TimeUnit.MILLISECONDS); SplashActivity.this.runOnUiThread(new Runnable() { public void run() { Log.d(TAG, "redirect"); redirect(); } }); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (TimeoutException e) { downloadFAQ.cancel(true); Log.d(TAG, "Task has benn canceled"); if (downloadFAQ.isCancelled()) redirect(); } } }).start(); } else { r = new Runnable() { public void run() { redirect(); } }; handler.postDelayed(r, 2500); } } private class DownloadFAQ extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { Log.d(TAG, "Execute task"); ServerAPI server = new ServerAPI(getApplicationContext()); server.serverRequest(ServerAPI.GET_FAQ, null); return null; } } private void redirect() { Intent i = new Intent(SplashActivity.this, TabsActivity.class); startActivity(i); finish(); } @Override protected void onDestroy() { super.onDestroy(); handler.removeCallbacks(r); } ``` }

Original source