Saturday, 16 February 2013

android.os.NetworkOnMainThreadException in android

Hello Droid Guys,

Yesterday , I found a strange issue, my application is running fine on most of
the android devices. But when I run the same application on Samsung S-3, S2,
Samsung Galaxy Duos it will be crashed and give me the error

"android.os.NetworkOnMainThreadException".


Caused by: android.os.NetworkOnMainThreadException
10-30 15:07:35.341: E/AndroidRuntime(732):  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)

In my application basically I am showing a alert dialog with an logout button and on click of logout
button I am calling the service. This functionality is not working on above few device
and crashes every time on clickinng the logout button .

After spending a lot of time on it I came to know that on device api version below 9 we 
can call or write  network call in Main UI Thread but above api version 9 it gives error
android.os.NetworkOnMainThreadException. This is happening my  case.

There are Two way to fix this Issue:
    1.Instead of writing network call in Main UI Thread use Async Task.
    2.Add below code into your activity class inside onCreate() method just  after the 

      setContentView(R.layout.activity_main).
      
     if( Build.VERSION.SDK_INT >= 9){
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy); 
     }



Hoping , this will helps you.
Enjoy Coding :)
   

Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

7 comments:

  1. Hi Mukesh,
    All the best for your blog.

    This is not good idea. Your solution should avoid executing network operations like webservices calls should be done in a separate thread.
    AsyncTask provides a easy to use facility. You can as well write custom thread using java and without using much using android threads.

    ReplyDelete
  2. Thanks for your suggestion.
    Yes , you right, thats why I mention the two way and the first one is via using AsyncTask.

    Also you notice that Android 3.0 (Honeycomb) StrictMode is configured to crash with a NetworkOnMainThreadException exception, if network is accessed in the user interface thread.

    Also , when I am showing an alert dialog on main thread(no nerwork call) this exception will occur, So for that I prefer to use StrictMode policy.

    ReplyDelete
  3. Also ,this helps you in making network access independent from background processing.

    ReplyDelete
  4. Hi Mukesh you can help me ? i need your help, please give me your email.... i want ask you something and solved my problem

    ReplyDelete
  5. Hello Ardian,
    My email id is mukesh421985@gmail.com

    ReplyDelete

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin