Search in This Blog

android.os.NetworkOnMainThreadException

Problem:

The runtime this error 
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AnroidBlockGuardPolicy.onNetwork(StrictMode.java
is the common error when working with online activity work in android.
how to remove it?

Solution:

You need to put below lines to solve this:
StrictMode.ThreadPolicy thpolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(thpolicy);

Example:

Use below code after setContentView(..);

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

And also use below code before  onCreate(..)

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {...}
Previous
Next Post »