Caused by: android.os.NetworkOnMainThreadException error resolution

  • 2020-06-01 11:00:09
  • OfStack

Don't write code Android hand were born for a long time, find out may not be able to run programs written before find yourself, also do not have what specific error message, just see 1 other useful error Caused by: android. os. NetworkOnMainThreadException, has examined the reasons on after 4.0 in the main thread to perform Http request will be submitted to the the wrong, is probably afraid Http request time is too long cause program of suspended animation.

The solution has two ideas, which are:

Method 1: ignore it and force it (strongly not recommended, but easy to modify)
Add the following code below setContentView(R.layout.activity_main) in the MainActivity file


if (android.os.Build.VERSION.SDK_INT > 9) {
 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
 StrictMode.setThreadPolicy(policy);
}

Method 2: use Thread, Runnable, Handler (recommended)
Do HTTP requests in Runnable without blocking the UI thread ~


public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 this.setContentView(R.layout.share_mblog_view);
 new Thread(runnable).start();
}

Handler handler = new Handler(){
 @Override
 public void handleMessage(Message msg) {
 super.handleMessage(msg);
 Bundle data = msg.getData();
 String val = data.getString("value");
 Log.i("mylog"," Request the results -->" + val);
 }
}

Runnable runnable = new Runnable(){
 @Override
 public void run() {
 //
 // TODO: http request.
 //
 Message msg = new Message();
 Bundle data = new Bundle();
 data.putString("value"," Request the results ");
 msg.setData(data);
 handler.sendMessage(msg);
 }
}

Attachment: another solution

Android 4.1 project: share The Times with sina weibo:
android. os. NetworkOnMainThreadException
After searching on the Internet, I know that it is because of version problem. After 4.0, I will report this error when executing Http request in the main thread. Maybe it is because I am afraid that the request time of Http is too long, resulting in the false death of the program. So the friends on the Internet also gave the corresponding solution, which is called the policy below the countermeasures:

1. Add the following code to the onCreate function in Activity that initiated the Http request:


// As shown in the StrictMode The document 
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().
detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().
detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

You can't see the StrictMode class if you're working on a project other than Android 4.0. I also used com_weibo_android.jar from the Internet. However, when the jar package is downloaded, it will be converted into an Android 4.0 project, and then the above two lines of code will be added to the onCreate() function of ShareActivity. This will prevent you from reporting the error.

2: Thread, Runnable and Handler are used:


public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  this.setContentView(R.layout.share_mblog_view);
  new Thread(runnable).start();
}

Handler handler = new Handler(){
  @Override
  public void handleMessage(Message msg) {
    super.handleMessage(msg);
    Bundle data = msg.getData();
    String val = data.getString("value");
    Log.i("mylog"," The result of the request is -->"  val);
  }
}

Runnable runnable = new Runnable(){
  @Override
  public void run() {
    //
    // TODO: http request.
    //
    Message msg = new Message();
    Bundle data = new Bundle();
    data.putString("value"," Request the results ");
    msg.setData(data);
    handler.sendMessage(msg);
  }
}


Related articles: