For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Sunday, January 18, 2015

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) {...}
Share:

0 comments:

Post a Comment

Add Dependent Scripts

   here is the code that allow to add dependent script :         <script>         const addDependentScripts = async function( scriptsT...

Ads Inside Post

Powered by Blogger.

Arsip