I created a chain of server-related operations and put them in a class called
OutgoingSync.java
I havent wrapped any threading around any of the network operations.
This is how I start the whole thing.
ExecutorService executorService = Executors.newFixedThreadPool(1);
executorService.submit(new Runnable() {
@Override
public void run() {
new OutgoingSync(context);
}
});
I am using ExecutorService because I want it to shutdown the threads for me, so I dont have to worry about that.
However, when the first network operation starts, I get a NetworkOnMainThread exception.
Here is a 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 is "main"
// And the following network-related code throws a NetworkOnMainThreadException (because it is run on the main thread)
}
}