I have an asynchTask
that is downloading a file, which is working fine. However I want to account for the internet connection dropping midway through the download, and then attempt to restart the download after the connection has been restored.
To do this I tried to put the downloading into a Runnable
so that if I catch an error I can just re-run the runnable after the connection has been restored.
When I try to run this I get an exception saying it is using the main thread(see logcat below), I'm calling the runnable from doInBackground()
so presumably it should be run on a separate thread, or is my reasoning wrong?
Posting code is not really possible as it is ~1000 lines of code, and I would rather be told why my reasoning is wrong than have someone fix the code for me as I'm more likely to learn from it.
Logcat
android.os.NetworkOnMainThreadException
E/BxmcGFT ( 3808): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
E/BxmcGFT ( 3808): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
E/BxmcGFT ( 3808): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
E/BxmcGFT ( 3808): at java.net.InetAddress.getAllByName(InetAddress.java:214)
E/BxmcGFT ( 3808): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
E/BxmcGFT ( 3808): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
E/BxmcGFT ( 3808): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
E/BxmcGFT ( 3808): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
E/BxmcGFT ( 3808): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
E/BxmcGFT ( 3808): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
E/BxmcGFT ( 3808): at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
E/BxmcGFT ( 3808): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
E/BxmcGFT ( 3808): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
E/BxmcGFT ( 3808): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
E/BxmcGFT ( 3808): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:75)
E/BxmcGFT ( 3808): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:136)
E/BxmcGFT ( 3808): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:90)
E/BxmcGFT ( 3808): at my.package.GetDownLoadFileTask$1.run(GetDownLoadFileTask.java:467)
E/BxmcGFT ( 3808): at android.os.Handler.handleCallback(Handler.java:615)
E/BxmcGFT ( 3808): at android.os.Handler.dispatchMessage(Handler.java:92)
E/BxmcGFT ( 3808): at android.os.Looper.loop(Looper.java:137)
E/BxmcGFT ( 3808): at android.app.ActivityThread.main(ActivityThread.java:4745)
E/BxmcGFT ( 3808): at java.lang.reflect.Method.invokeNative(Native Method)
E/BxmcGFT ( 3808): at java.lang.reflect.Method.invoke(Method.java:511)
E/BxmcGFT ( 3808): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
E/BxmcGFT ( 3808): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/BxmcGFT ( 3808): at dalvik.system.NativeStart.main(Native Method)
the line giving the error is this
androidHttpTransport.call(soapUri, envelope);
Again question is why is the runnable being run on the main thread when it is called from doInBackground()
? and would this be better done on a thread rather than an AsynchTask
?