Error Strictmode$Androidblockguardpolicy.Onnetwork

Error StrictMode$AndroidBlockGuardPolicy.onNetwork

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

Edit:
This answer "solves" the "problem", but it does not really explain it, which is the important part.

Modifying StrictMode is NOT a fix. As well as this exception is not really an error in program logic. It is and error in program paradigm and the exception tells you, that

you should not be performing http calls on your main thread as it leads to UI freezing. As some other answers suggests, you should hand off the http call to AsyncTask class.

https://developer.android.com/reference/android/os/AsyncTask

Changing the StrictMode really just tells the app - do not remind me of me writing bad code. It can work for debugging or in some cases where you know what you are doing but generally it is not the way to go.

Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`

Firstly, StrictMode is something you explicitly turned on from code, it is just a debugging aid, and should not be left on in production code.

Secondly, calling .doInBackground manually means running it on the UI thread. If you use AsyncTask as intended, you get its return value as a parameter in .onPostExecute.

android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)

Your doInBackground calls runOnUiThread, effectively pushing the operation from the background thread right back onto the UI thread. Remove that and move your UI updating code to onPostExecute.

android.os.NetworkOnMainThreadException

you can put the below code in oncreate. but it wont solve the problem it will only hide it.

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

what you actually need to do is put network related code in your createExpandableListViewDialog();(the http request part) inside a background thread.

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

Remove your runOnUiThread() code from doInBackground(), that's not how AsyncTask is meant to work

runOnUiThread(new Runnable() {
public void run() {

Every method in an AsyncTask except for doInBackground() runs on the UI Thread. Therefore, do your network stuff in doInBackground() and use the other methods as needed.

onProgressUpdate() can be used to update the UI during execution of doInBackground()

onPostExecute() can be used to receive a result from doInBackground() and update the UI accordingly

onPreExecute() can be used to update the UI before doInBackground() runs for things such as showing a ProgressDialog

Please read through the AsyncTask Docs several times. It is a little tricky at the beginning but once you understand how its meant to work then it can be a great thing.

How to resolve android.os.NetworkOnMainThreadException?

The Android OS does not allow heavy process to execute in the main thread/UI Thread because the application will slow down, decreasing performance and the application will lag.

However, you can execute it in an AsyncTask as shown here. Do your process/call your function in the doInBackground of this asyncTask.

private class Download extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;

@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("Hi", "Download Commencing");

pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Downloading Database...");

String message= "Executing Process";

SpannableString ss2 = new SpannableString(message);
ss2.setSpan(new RelativeSizeSpan(2f), 0, ss2.length(), 0);
ss2.setSpan(new ForegroundColorSpan(Color.BLACK), 0, ss2.length(), 0);

pDialog.setMessage(ss2);

pDialog.setCancelable(false);
pDialog.show();
}

@Override
protected String doInBackground(String... params) {

//INSERT YOUR FUNCTION CALL HERE

return "Executed!";

}

@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("Hi", "Done Downloading.");
pDialog.dismiss();

}
}

and call it as such: new Download().execute(""); from another function.

You can do away with the Progress Dialog. I personally like it because I know my process will finish (like data loading) before the user can do anything, ensuring that no error occurs when the user interacts with the program.

android.os.NetworkOnMainThreadException when the socket code seems to be executed on another thread

Use start() and not run() to actually start a new thread.



Related Topics



Leave a reply



Submit