0

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?

Cob50nm
  • 911
  • 2
  • 9
  • 27
  • @Raghunandan I have read that thread and if you read mine you would see that it is not a duplicate. – Cob50nm Aug 29 '14 at 15:17
  • 2
    your exception is the same as the one posted in the link. Its clear you are running network operation on the main ui thread. By the way you haven't posted the relevant code. – Raghunandan Aug 29 '14 at 15:19
  • 1
    The exception is not thrown from within your doInBackground. It is not in the stacktrace. You are doing something else wrong – hoomi Aug 29 '14 at 15:20
  • Agreed, the error is the same but thats not what I'm asking about. I'm asking why it is on the main thread when it is being called from doInBackground which should be on a seperate thread. and again if you had read the post instead of just marking it as a duplicate you would know why I didnt post code. – Cob50nm Aug 29 '14 at 15:21
  • @Cob50nm Quoting **Posting code is not really possible as it is ~1000 lines of code** . Well you need not post all those lines. The ones that are relevant like `doInbackground`. – Raghunandan Aug 29 '14 at 16:36
  • @Cob50nm do read http://stackoverflow.com/help/how-to-ask – Raghunandan Aug 30 '14 at 06:47
  • @Cob50nm and you could also post how you invoked the asynctask – Raghunandan Aug 31 '14 at 12:48
  • It might be a bit too late, but let me use my crystal ball... hm... yes... I see. You're calling `post(yourRunnable)` from `doInBackground()`. This in fact executes operation on the main thread. – Dmitry Zaytsev Jun 07 '16 at 12:54

0 Answers0