java - I sumbitted a chain of network operations to newFixedSizeThreadPool and I got NetworkOnMainThreadException -


i created chain of server-related operations , put them in class called

outgoingsync.java 

i havent wrapped threading around of network operations.

this how start whole thing.

executorservice executorservice = executors.newfixedthreadpool(1);                 executorservice.submit(new runnable() {                      @override                     public void run() {                         new outgoingsync(context);                      }                 }); 

i using executorservice because want shutdown threads me, dont have worry that.

however, when first network operation starts, networkonmainthread exception.

here code snippet:

public class outoingsync {      public outgoingsync(context context){          log.e("outgoingsync thread", thread.currentthread.getname()); // output "pool-2,thread-1"         dosomestuff();     }      private void dosomestuff() {         new uploadphotostoserver();     }  }  public class uploadphotostoserver {     public uploadphotostoserver() {         log.e("upload photos thread", thread.currentthread.getname()); // output "main" // , following network-related code throws networkonmainthreadexception (because run on main thread)     } } 

from documentation

the exception thrown when application attempts perform networking operation on main thread

so, can try running code in asynctask . executeonexecutor() method can trick.

http://developer.android.com/reference/android/os/asynctask.html#executeonexecutor(java.util.concurrent.executor, params...)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -