Exception coming android os NetworkOnMainThreadException

0 votes
I came up with this exception "android.os.NetworkOnMainThreadException " while running my code with RssReader. How can i fix this issue?
RssHandler theRSSHandler = new RssHandler();
Jun 6, 2018 in Java by Avi
• 160 points
1,441 views

1 answer to this question.

0 votes

This exception is thrown when an application attempts to perform a networking operation on its main thread. 

Run this sample code:

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;
        } finally {
            is.close();
        }
    }

    protected void onPostExecute(RSSFeed feed) {
        // TODO: check this.exception
        // TODO: do something with the feed
    }
}
answered Jun 6, 2018 by poorvi
• 160 points

Related Questions In Java

0 votes
1 answer

Fixing android.os.NetworkOnMainThreadException in Java

Android has some good tips on good ...READ MORE

answered Jun 12, 2018 in Java by Rishabh
• 3,620 points
5,573 views
0 votes
1 answer

Error: 'android.os.NetworkOnMainThreadException'?

Hello, I solved this problem using a new Thread. Thread ...READ MORE

answered May 28, 2020 in Java by Niroj
• 82,840 points
1,772 views
0 votes
0 answers

How can I fix 'android.os.NetworkOnMainThreadException'?

When I run the Android project for ...READ MORE

May 23, 2022 in Java by Kichu
• 19,040 points
1,220 views
0 votes
2 answers
0 votes
1 answer

To use JavaMail API for sending mails in Android without using the default/built-in App

ADD 3 jars found in the following ...READ MORE

answered Jun 6, 2018 in Java by sridhar
• 160 points
1,474 views
0 votes
1 answer

How to call a method after a delay in Android using Java?

final Handler handler = new Handler(); handler.postDelayed(new Runnable() ...READ MORE

answered Jun 11, 2018 in Java by Akrati
• 3,190 points
5,357 views
0 votes
1 answer

What is ArrayIndexOutOfBoundsException ?

If You check System.out.print(names.length); you will get 3 ...READ MORE

answered May 11, 2018 in Java by sharth
• 3,370 points
965 views
0 votes
2 answers

How can I solve java.lang.NoClassDefFoundError in Java?

NoClassDefFoundError means that the class is present ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,920 points
34,138 views
0 votes
1 answer

How can we resolve ClassNotFoundException in Java?

Your classpath is broken. Depending on how you ...READ MORE

answered May 22, 2018 in Java by Akrati
• 3,190 points
1,257 views
0 votes
1 answer

NoClassDefFoundError in Java

Can’t tell you the exact reason as ...READ MORE

answered May 25, 2018 in Java by v.liyyah
• 1,300 points
2,520 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP