What is NetworkOnMainThread Exception?

This exception is occurs when we call the network calls in Main Thread.
Long Running process never run on Main Thread.
To solve this problem, having following ways

1) Strict mode:

write this code in your onCreate method

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

StrictMode.setThreadPolicy(policy);

 

2) AsynkTask
write your code inside doInBackground() method

 

 private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
     protected Long doInBackground(URL... urls) {
         
// write your code here
         return ;
     } 
};
3) Using Thread 
 Thread thread = new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            //Your code goes here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start(); 


Subscribe For Daily Updates

Flutter Questions
Android Questions