Wednesday 29 June 2016

android - Error StrictMode$AndroidBlockGuardPolicy.onNetwork




I created android application and use JSON get data from server but I got the error show below:



android.os.NetworkOnMainThreadException

at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)


how can I fix this problem
Thank you


Answer



you have to insert 2 lines "StrictMode" on MainActivity Class, example's below:



public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

try {
// JSON here
} catch (JSONException e2) {
// TODO Auto-generated catch block

e2.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


setContentView(R.layout.activity_main);
Intent intent=new Intent(this,HomeActivity.class);

startActivity(intent);
}
}


Aey.Sakon


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...