Ads Inside Post

Search in This Blog

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
}
//////////////////////////////////////////