Sunday 16 June 2019

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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...