XML : RSSFeed-App FATAL EXCEPTION: main [duplicate]

This question already has an answer here:

I found an instruction in the internet how to create an application which is able to read rss feeds.

The problem is, when I want to start the app, it just show me a little dialog, like "Unfortunately, "RSSFeed" has stopped." and the LogCat looks like this:

  11-28 23:32:26.954: E/AndroidRuntime(30240): FATAL EXCEPTION: main  11-28 23:32:26.954: E/AndroidRuntime(30240): Process:  gym.ntz.project.blu.smv.app.rssfeed, PID: 30240  11-28 23:32:26.954: E/AndroidRuntime(30240): java.lang.RuntimeException: Unable to start activity ComponentInfo{gym.ntz.project.blu.smv.app.rssfeed/gym.ntz.project.blu.smv.app.rssfeed.RSSFeed}: android.os.NetworkOnMainThreadException  ....    

Here's my code:

  package gym.ntz.project.blu.smv.app.rssfeed;      import android.app.Activity;  import android.os.Bundle;  import android.widget.TextView;  import javax.xml.parsers.SAXParser;  import javax.xml.parsers.SAXParserFactory;  import java.net.URL;  import org.xml.sax.InputSource;  import org.xml.sax.XMLReader;  import java.io.IOException;  import org.xml.sax.SAXException;  import javax.xml.parsers.ParserConfigurationException;  import org.xml.sax.Attributes;  import org.xml.sax.helpers.DefaultHandler;    public class RSSFeed extends Activity {      /** Called when the activity is first created. */      String rssResult = "";      boolean item = false;          public void onCreate(Bundle savedInstanceState)       {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_rssfeed);          TextView rss = (TextView) findViewById(R.id.rss);          try {              URL rssUrl = new URL("http://www.javaworld.com/index.xml");              SAXParserFactory factory = SAXParserFactory.newInstance();              SAXParser saxParser = factory.newSAXParser();              XMLReader xmlReader = saxParser.getXMLReader();              RSSHandler rssHandler = new RSSHandler();              xmlReader.setContentHandler(rssHandler);              InputSource inputSource = new InputSource(rssUrl.openStream());              xmlReader.parse(inputSource);            } catch (IOException e) {rss.setText(e.getMessage());          } catch (SAXException e) {rss.setText(e.getMessage());          } catch (ParserConfigurationException e) {rss.setText(e.getMessage());          }            rss.setText(rssResult);      }        private class RSSHandler extends DefaultHandler {             public void startElement(String uri, String localName, String qName,                  Attributes attrs) throws SAXException {              if (localName.equals("item"))                  item = true;                if (!localName.equals("item") && item == true)                  rssResult = rssResult + localName + ": ";            }            public void endElement(String namespaceURI, String localName,                  String qName) throws SAXException {            }            public void characters(char[] ch, int start, int length)                  throws SAXException {              String cdata = new String(ch, start, length);              if (item == true)                  rssResult = rssResult +(cdata.trim()).replaceAll("\\s+", " ")+"\t";            }        }  }    

I used breakpoints to find out what could cause the problems and find out that the app is stopped at this line:

   InputSource inputSource = new InputSource(rssUrl.openStream());    

But I have no idea how to fix it.

What could be the problem? Please, help me.

No comments:

Post a Comment