9

I need to execute third-party open source program, which throws NetworkOnMainThreadException. According to SDK reference, this is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads.

On the first stage I just want to run the program, without changing the source. So, I changed the line in AndroidManifesr.xml from:

    android:targetSdkVersion="15"

to:

    android:targetSdkVersion="10"

However, this doesn't help, and program still throws NetworkOnMainThreadException. How can I make this to work? I am trying to execute the program on Android Emulation Google APIs (level 16).

Mr.Me
  • 9,192
  • 5
  • 39
  • 51
Alex F
  • 42,307
  • 41
  • 144
  • 212

3 Answers3

21

You could change it to:

android:targetSdkVersion="9"

API 10 corresponds to honeycomb, while 9 is gingerbread. This behavior is only seen in APIs 10 and above.

However, I would advise against this. Instead, you should move any long running operations, or operations with the possibility of running for long into a background thread, like an AsyncTask.

You could also try to set Strict Mode off using:

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
  • 1
    I agree that this is a poor workaround but it definitely identified why this started happening suddenly when I updated an older project. – RevNoah Jun 05 '13 at 18:01
4

You are performing network task on your UI Thread. The best way to perform network operation is to use AsyncTask or simply create another thread. However, if you want a quick and dirty way to get rid of the error; you can also use this

ThreadPolicy tp = ThreadPolicy.LAX;
StrictMode.setThreadPolicy(tp);
Infinity
  • 3,695
  • 2
  • 27
  • 35
  • This is cleaner than the other "setThreadPolicy" option in the accepted answer, and was exactly what I needed. There are in fact instances where network on the main thread is desired, and my particular case for this was sending a crash report to a server. – Josh Mar 31 '14 at 16:24
0

These are not solutions, you Just tell the tutelar to sleep and sill!!! the android decide not to let you make network operations in the main thread (UI thread), because of the "halts" that these operation can make (the ui stop respond). So, as I read, they tell you to not shoot your leg, and you find the way to do!!!

the solution : separated thread, Or Async ...

EsmaeelQash
  • 488
  • 2
  • 6
  • 20
  • 1
    There are in fact instances where you want or need to use the main thread for network operations. One example: sending a crash report when your main thread throws an uncaught exception. If you want to send it to a remote server for logging, you can't spin up a new thread and expect that thread to live long enough to send the log without having it block on the main thread. I'm sure there are other cases as well. – Josh Mar 31 '14 at 16:26
  • it is clear and not complex, the documentations tell you should not make network operations in the main thread. my answer is not wrong, so why the -1 ?? just because you imagine setuation that need network in main?? – EsmaeelQash Mar 31 '14 at 20:08
  • EsmaeelQuash, it is an invalid statement to say that the other replies "are not solutions". That is why the -1.The other solutions are in fact correct solutions for instances where you need this. It is not recommended to do network activity on the main thread, but that does not mean there are not valid reasons to do so that require valid solutions. – Josh Apr 01 '14 at 20:34
  • OK, I mean, if the OS refuse to do that (Network in main), it mean that it's not recommended to turn around and Do. the programmer tells the OS that he is using previous version as target to let him do "illegal" Operation!!! is this solution? I tell the correct way (separated thread). but if it necessary to do network in main this is another issue. – EsmaeelQash Apr 05 '14 at 11:58