---
title: "How to fix \"android.os.NetworkOnMainThreadException\"?"  
description: "How to fix \"android.os.NetworkOnMainThreadException\"?"  
author: "Utpal Vishwas"  
published: 2023-07-21  
updated: 2023-07-22  
canonical: https://www.mindstick.com/forum/159221/how-to-fix-android-os-dot-networkonmainthreadexception  
category: "android"  
tags: ["exception handling", "android", "exception"]  
reading_time: 3 minutes  

---

# How to fix "android.os.NetworkOnMainThreadException"?

How to [fix](https://yourviews.mindstick.com/view/80763/donald-trump-ki-jeet-fix-hai) '[android.os.NetworkOnMainThreadException](https://www.mindstick.com/forum/23124/what-is-android-os-dot-networkonmainthreadexception)'?

## Replies

### Reply by Aryan Kumar

The `android.os.NetworkOnMainThreadException` exception is thrown when an attempt is made to perform a network operation on the main thread. This is because the main thread is responsible for handling user interaction, and performing network operations on the main thread can cause the application to become unresponsive.

There are a few ways to fix the `android.os.NetworkOnMainThreadException` exception:

- **Use an AsyncTask.** An AsyncTask is a class that allows you to perform long-running tasks in the background, without blocking the main thread. To use an AsyncTask, you need to extend the AsyncTask class and implement the following methods:

   - `onPreExecute()`: This method is called before the background task is executed.
   - `doInBackground()`: This method is called on a background thread and is where you should perform the network operation.
   - `onPostExecute()`: This method is called after the background task is finished and is where you should update the UI.

Here is an example of how to use an AsyncTask to perform a network operation:

```plaintext
class MyAsyncTask extends AsyncTask<String, Void, String> {

  @Override
  protected void onPreExecute() {
    // Do something before the background task is executed
  }

  @Override
  protected String doInBackground(String... params) {
    // Perform the network operation
    return "The response from the network";
  }

  @Override
  protected void onPostExecute(String result) {
    // Update the UI with the result of the network operation
    textView.setText(result);
  }
}
```

- **Use a thread pool.** A thread pool is a group of threads that are managed by the system. You can use a thread pool to execute network operations in the background, without blocking the main thread. To use a thread pool, you need to create a ThreadPoolExecutor object and then submit your network operation to the thread pool.

Here is an example of how to use a thread pool to perform a network operation:

```plaintext
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 5, 10000, TimeUnit.MILLISECONDS);

threadPoolExecutor.execute(new Runnable() {

  @Override
  public void run() {
    // Perform the network operation
  }
});
```

- **Disable StrictMode.** StrictMode is a feature of Android that can be used to detect potential problems with your application. One of the problems that StrictMode can detect is the use of network operations on the main thread. To disable StrictMode, you need to call the `setThreadPolicy()` method on the `StrictMode` object.

Here is an example of how to disable StrictMode:

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

It is important to note that disabling StrictMode can make your application more vulnerable to problems. Therefore, you should only disable StrictMode if you are sure that you understand the risks involved.


---

Original Source: https://www.mindstick.com/forum/159221/how-to-fix-android-os-dot-networkonmainthreadexception

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
