0

So I tried to set up an RSS feed for my Android application with the attached code, yet when I run it on the emulator, it crashes and I receive the attached errors in my LogCat. Any ideas for what could be wrong? Thanks in advance!!!

package com.fixed_gear_app;

import java.io.IOException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class RSSFeed extends Activity {
     /** Called when the activity is first created. */
    String rssResult = "";
    boolean item = false;

    @Override
    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("https://www.facebook.com/feeds/page.php?format=rss20&id=619608694724497");
            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);
    }
    /**public String removeSpaces(String s) {
          StringTokenizer st = new StringTokenizer(s," ",false);
          String t="";
          while (st.hasMoreElements()) t += st.nextElement();
          return t;
        }*/
    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";

        }

    }
}

LogCat

Paradox
  • 4,602
  • 12
  • 44
  • 88

1 Answers1

0

You are attempting to do network communication on the main Activity thread. Android, by default, disallows this, (as you do not know how long it can take) so you will have to move your networking code to a class extending AsyncTask. Have a read of this, which shows you how to put it on an AsyncTask.

the6p4c
  • 644
  • 5
  • 17
  • I'll check that out and see if I can get it to work! – Paradox Apr 09 '14 at 23:13
  • I found that when I created a separate AsyncTask Class, I could not use the findViewByID() method (that I need) which is part of the Activity class. Any suggestions? – Paradox Apr 09 '14 at 23:31
  • @Paradox You will need to have a variable in your class for the view you want to change (you can set this before you execute the task, via a method or direct variable setting), then you call `findViewByID()` on that view, using `(name of view).findViewByID(...)`. – the6p4c Apr 09 '14 at 23:35
  • My problem is that findViewById() is unavailable in my extension of the AsyncTask class though. (hopefully that wasn't a stupid response; I don't have much experience) – Paradox Apr 09 '14 at 23:44
  • @Paradox You are right. However, if you construct the AsyncTask class as a variable, set a variable INSIDE that class to the view which contains the thing you want to change, then execute the task, you can use `findViewById` on the variable inside that class. Have a look at [this](http://stackoverflow.com/a/14164318/3492369) other StackOverflow answer. – the6p4c Apr 09 '14 at 23:49