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".
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 :)
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 :)
Hi Mukesh,
ReplyDeleteAll 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.
Thanks for your suggestion.
ReplyDeleteYes , 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.
Also ,this helps you in making network access independent from background processing.
ReplyDeleteThank u
ReplyDeleteHi Mukesh you can help me ? i need your help, please give me your email.... i want ask you something and solved my problem
ReplyDeleteHello Ardian,
ReplyDeleteMy email id is mukesh421985@gmail.com
thanks
ReplyDelete