The android.os.NetworkOnMainThreadException exception

In this article I explain a possible cause of android.os.NetworkOnMainThreadException and how to avoid it.
From the Android site you can read:
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher…

Here is a sample developed for Gingerbread, API level 9:

  1. create an Android project called HttpClient
  2. edit the file AndroidManifest.xml

    where <uses-sdk android:minSdkVersion="9" /> means an API version earlier to Honeycomb (Gingerbread, API level 9), and <uses-permission android:name="android.permission.INTERNET"/> authorizes the application to perform an internet connection
  3. edit the file

This app is executed without errors.

If you specify an API level after Honeycomb, such as Ice Cream Sandwich, replacing the line <uses-sdk android:minSdkVersion="9" /> with <uses-sdk android:minSdkVersion="14" /> and you launch the application, you get the exception android.os.NetworkOnMainThreadException.

An easy way to avoid the exception is to insert the following code (which requires import android.os.StrictMode):

before the row connect() in
But this method is recommended in development environments only, the recommended method is to use the class AsyncTask.

An example is the following in which the code of the class is replaced by:

You can override not only doInBackground but also other methods of the AsyncTask class like OnPreExecute(), OnPostExecute(Result), publishProgress(Progress. ..).


 Add your comment
  1. Thanks! helpful helpful helpful

  2. very helpful! thank you.

  3. You must be add this command
    ” if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    into protected void onCreate(Bundle savedInstanceState) for check program.

    something like this.

    protected void onCreate(Bundle savedInstanceState) {
    // sart this coding.——————————–
    if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    // end coding ————————————

  4. Thank you very much for help it is working well

  5. Grazie, hai aiutato anche me! Se usate Xamarin (quindi C#), la sintassi equivalente corretta è:

    private class Connection : AsyncTask {
    protected override Java.Lang.Object DoInBackground(params Java.Lang.Object[] @params) {
    return null;

  6. thank you so much for this¡¡ it worked after so much time spent on thinking how to solve it

  7. Thanks for solving my problem. i appreciate for your great help

  8. Thanks
    It works for me.
    I have tried anything i get but none of that works.

  9. Grazie amico, mi hai aiutato molto con la mia tesi di laurea! 😀

  10. Gracias amigo.
    he perdido mas de 10 horas con este problema
    Al fin quedo solucionado gracias a ti.!!!!
    eress lo maximo. sigue asi.

  11. Thank you!

    You helped me after trying this crap (with MySQL Connection) for more than 10 hours!
    I am so grateful!


    Greets from Austria,

  12. “An easy way to avoid the exception is to insert the following code (which requires import android.os.StrictMode):”
    Ciao Luca, complimenti per il blog. L’ho conosciuto per sbaglio tramite stackoverflow. Uno degli utenti ha postato questo post come possibile soluzione a un NetworkOnMainThreadException. Dato che utilizzare le policy StrictMode non e’ la soluzione, potresti modificarlo?

  13. thanks…you saved my last chance for my thesis project…

  14. Muy buen articulo, sobre la fuente del problema y como resolverlo
    saludos desde mexico

  15. Thanks man,,,your article helped me a lot. Keep up the good work

  16. Nice post.. its helped me lot.

  17. Grande articolo!! ho perso 2 giorni per quel problema!

  18. nice one. Thnx…..

  19. khupach chaan article aahe…nice one article!!!

  20. Questo problema mi ha tolto il sonno per due giorni interi. Grazie per questo bel post!

  21. Grande mi hai salvato…
    e quando lo trovavo mai sto problema 🙂

  22. grazie, ti devo almeno un paio d’ore della mia vita

  23. thank you guy! this is very good for me, i’m gonna crazy!

  24. Many thanks for this very useful example ….Grazie

  25. Hi! Thanks for this very helpful post!

  26. grazie mille stavo impazzendo non riuscivo a capire il perchè
    mi hai illuminato

  27. nice article!!!!!!

Leave a Comment

Your email address will not be published.