For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Friday, September 26, 2014

android.os.NetworkOnMainThreadException


android.os.NetworkOnMainThreadException


if you work with android and want to get data from any url by using URL and  URLConnection you me stuck by the "android.os.NetworkOnMainThreadException" error message while run the app.

To over come this run time error message you use "StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();  StrictMode.setThreadPolicy(policy);" as listed below.

Keep in mind that use it in onCreate(Bundle savedInstanceState) after "setContentView(R.layout.activity_main);" method

//////////////////////////////////////////
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//// other code here
}
//////////////////////////////////////////

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

Blog Archive