---
title: "What is android.os.NetworkOnMainThreadException"  
description: "What is android.os.NetworkOnMainThreadException"  
author: "Samuel Fernandes"  
published: 2015-04-23  
updated: 2015-04-23  
canonical: https://www.mindstick.com/forum/23124/what-is-android-os-dot-networkonmainthreadexception  
category: "android"  
tags: ["android", "thread", "exception"]  
reading_time: 2 minutes  

---

# What is android.os.NetworkOnMainThreadException

In the below [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) I got an [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) when running my [Android](https://www.mindstick.com/articles/13046/10-reasons-why-i-prefer-android-vs-iphone-ios) [project](https://www.mindstick.com/articles/105927/how-to-excel-at-managing-multiple-projects) for RssReader.\
And it [shows](https://yourviews.mindstick.com/view/81380/new-york-violent-shooting-shows-people-are-not-safe-in-night-life) an error:android.os.NetworkOnMainThreadException\
\
How can I [fix this issue](https://answers.mindstick.com/qa/94675/google-assistant-saying-something-went-wrong-how-to-fix-this-issue)?\

```
URL url = new URL(urlToRssFeed);SAXParserFactory factory = SAXParserFactory.newInstance();SAXParser parser = factory.newSAXParser();XMLReader xmlreader = parser.getXMLReader();RssHandler theRSSHandler = new RssHandler();xmlreader.setContentHandler(theRSSHandler);InputSource is = new InputSource(url.openStream());xmlreader.parse(is);return theRSSHandler.getFeed();
```

## Replies

### Reply by Anonymous User

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in **AsyncTask**:

```
class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> {    private Exception exception;    protected RSSFeed doInBackground(String... urls) {        try {            URL url= new URL(urls[0]);            SAXParserFactory factory =SAXParserFactory.newInstance();            SAXParser parser=factory.newSAXParser();            XMLReader xmlreader=parser.getXMLReader();            RssHandler theRSSHandler=new RssHandler();            xmlreader.setContentHandler(theRSSHandler);            InputSource is=new InputSource(url.openStream());            xmlreader.parse(is);            return theRSSHandler.getFeed();        } catch (Exception e) {            this.exception = e;            return null;        }    }    protected void onPostExecute(RSSFeed feed) {        // TODO: check this.exception         // TODO: do something with the feed    }}
```

\
How to execute the task: In MainActivity.java file you can add this line within your oncreate() method\

```
new RetrieveFeedTask().execute(urlToRssFeed);
```

Don't forget to add this to AndroidManifest.xml file:\

```
<uses-permission android:name="android.permission.INTERNET"/>
```


---

Original Source: https://www.mindstick.com/forum/23124/what-is-android-os-dot-networkonmainthreadexception

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
