android.os.NetworkOnMainThreadException while using Android Project for RssReader

I am using the below code snippet to establish the network connection while using the android app in the main() function

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();

But when creating instance of SAXParserFactory, an exception is generated as

android.os.NetworkOnMainThreadException

I am not able to resolve this as i am not getting any associated stack trace of exception generation with this exception. Any idea how to resolve this ?