1

I'm trying to handle the situation where the user loses their data connection during an Http request in an AsyncTask. Currently the AsyncTask will never finish executing if this happens and the app just stalls. I can't even check isCancelled() because the Http request just stalls out on one function call, so I can't implement a loop or anything.

I'm using the following in the UI thread but I can't figure out how to check if the task has been cancelled from within the task:

    task = new LoginTask();
    task.execute(this, mUsername, mPassword);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable()
    {
        @Override 
        public void run()
        {
            if (task != null && task.getStatus() == AsyncTask.Status.RUNNING)
            {
                task.cancel(true);
            }
        }
    }, 5000);
ashishduh
  • 6,629
  • 3
  • 30
  • 35
  • ping the location before trying to open it via an async task? see if you have any other interfaces available except 127.0.0.1? – Shark Jul 10 '13 at 14:44
  • 1
    Not sure how you are getting your data but most of the network/http calls support setting a timeout. `HttpConnectionParams` for example. – Ken Wolf Jul 10 '13 at 14:45
  • @Shark I do test for an active connection before making the call but the call takes about 5 seconds to execute and the user can lose connection in that time. – ashishduh Jul 10 '13 at 14:48
  • @KenWolf I'm using the KSoap 2 library because it has a wrapper for NTLM authentication, which I need. I don't think it supports any setting of a timeout. – ashishduh Jul 10 '13 at 14:50
  • See if this helps: http://stackoverflow.com/questions/11348100/handling-timeout-while-using-ksoap2-in-android – Ken Wolf Jul 10 '13 at 14:53
  • Well, the KSoap library is written in java... Worst case scenario, you could abstract one of the classes and manually set a timeout on the HttpConnection... – Joel Jul 10 '13 at 15:14
  • @Joel This is what it looks I need to end up doing. Looking at the code, it looks like it takes a timeout parameter but doesn't actually set a timeout on the connection. – ashishduh Jul 10 '13 at 16:11

2 Answers2

0

In order to stop processing, you would need to do a check on isCancelled() inside doInBackground(). If there are any reasonable places to do so in your doInBackground, you can add an if-statement. Otherwise you should consider setting a timeout on the http connection.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
0

Since KSoap 2 is written in Java, an option for you would be to abstract one of the classes and manually set a timeout on the HttpConnection.

Based on our prior discussion, you will probably have to override the method that doesn't set a timeout on the connection and replace it with your own method and a connection that times out. Should be managable thanks to KSoap being open source! Good luck.

Joel
  • 4,732
  • 9
  • 39
  • 54