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

How to make center align child div

 How to make center align child div?   Suppose you have 2 Div tag. And want the make inner tag center <div id="d1">      ...

Ads Inside Post

Powered by Blogger.

Arsip

Blog Archive