Android Error – android.os.NetworkOnMainThreadException

In this android programming tip of the day, we are going to explore android exception also known as android.os.NetworkOnMainThreadException and how to fix it.

For seasoned android developer, android.os.NetworkOnMainThreadException is something they are use to and they can spot where it wil happen but this is a different case for beginners android developers

The main cause of android.os.NetworkOnMainThreadException is when you try to make a network call in UI Thread or time taken processing. Making network call in UI Thread is a bad practice since network call will take a long time to return a response and this will invariable crash your app due to UI Thread blockage.

In other to avoid android.os.NetworkOnMainThreadException, endeavor to make every network call in non UI Thread.

There are different ways to achieve this but the option you choose will depend on your user case or project requirement.

Like for example, there are situations where the network call response object will be used to update View controls in main UI Thread.

If you fail to return to the UI Thread for the update, you will face the following android exception – ViewRootImpl$CalledFromWrongThreadException.

We can solve android.os.NetworkOnMainThreadException with any of the following solutions.

 

Use of AsyncTask

You can create a class that extends the default android AsyncTask class. The class must override the doInBackground() method.

Inside the doInBackground() method is where you will make your network calls. If you plan to update any View control in the UI thread then you should do that task in onPostExecute() method.

Below is a custom AsyncTask class.

<uses-permission android:name="android.permission.INTERNET"/>
public class MyDownloader extends AsyncTask<String,Void,Bitmap>

{

@Override

protected void onPreExecute() {

super.onPreExecute();

}

@Override

protected void onPostExecute(Bitmap bitmap) {

super.onPostExecute(bitmap);

}

@Override

protected Bitmap doInBackground(String... params) {

return result;

}

}

}

 

Create a new Thread

Another option is to create a new thread and make your network calls on the created thread.

You can achieve this with the solution below.

new Thread(new runnable(){

@Override

public void run() {

// Do network action in this function

}

}).start();

If you plan to update a View control in UI thread from another thread, you can call runOnUiThread() method and update the view inside the run() method.

Finally, you must remember to add Internet permission whenever you are making a network call. This is how to do it if you do not know.

<uses-permission android:name="android.permission.INTERNET"/>

This is the end of the android programming tips. If you have any question, kindly use the comment box below and I will try as much as possible to provide a solution.

Remember to subscribe with your email address to be among the first to receive my new android blog post once it is published.

OTHER INTERESTING POSTS:

Add a Comment