user3234413
user3234413

Reputation: 11

RSS Feed Library not working

I'm using this library in my android project to read an RSS Feed:

https://github.com/matshofman/Android-RSS-Reader-Library

Right now I'm just starting off and I am trying to get any RSS feed to work and give out a single String (a Title) and show it in a textview. If that works, I'll go on to add it in a listview then open in a different window.

But as it is, the code below does not work:

        TextView t = (TextView) findViewById(R.id.textView1);
    try {
         url = new URL("http://feeds.feedburner.com/ndtv/TqgX");
    } catch (Exception e) {
        t.setText("URLerror");
    }
    try {
        RssFeed feed = RssReader.read(url);
        ArrayList<RssItem> rssItems = feed.getRssItems();
        String sn = feed.getTitle();
        t.setText(sn);
        for (RssItem rssItem : rssItems) {
            Log.i("RSS Reader", rssItem.getTitle());
        }
    } catch (Exception e) {
        Log.i("Error", "eror");
        t.setText("RSSerror");
    }

On running the Textview gets set to "RSSerror" so the problem is in the 2nd try block. Below is the official example from the library:

   URL url = new URL("http://example.com/feed.rss");
RssFeed feed = RssReader.read(url);

ArrayList<RssItem> rssItems = feed.getRssItems();
for(RssItem rssItem : rssItems) {
    Log.i("RSS Reader", rssItem.getTitle());
}

Am I using the wrong format for the RSS URL? or something else?

Here's the RSSReader class:

public class RssReader {

public static RssFeed read(URL url) throws SAXException, IOException {

    return read(url.openStream());

}

public static RssFeed read(InputStream stream) throws SAXException, IOException {

    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        RssHandler handler = new RssHandler();
        InputSource input = new InputSource(stream);

        reader.setContentHandler(handler);
        reader.parse(input);

        return handler.getResult();

    } catch (ParserConfigurationException e) {
        throw new SAXException();
    }

}

I've got 3 others, RSSItem, RSSHandler and RSSFeed which are, as far as I know, working great. What could be causing the problem?

If you need the other classes, kindly ask (Alternatively, look up the giuhub project)

Any input will be appreciated :-)

Upvotes: 1

Views: 2590

Answers (1)

Pkmmte
Pkmmte

Reputation: 2868

Try this library: https://github.com/Pkmmte/PkRSS

It works with most RSS feeds by default and downloads efficiently but it also allows you to plug in your own Parser and make it.

For your code, something like this would work:

TextView t = (TextView) findViewById(R.id.textView1);
try {
    url = new URL("http://feeds.feedburner.com/ndtv/TqgX");
} catch (Exception e) {
    t.setText("URLerror");
}
try {
    List<Article> rssItems = PkRSS.with(this).load(url).get();
    Article article = rssItems.get(0);
    t.setText(article.getTitle());
    for (Article item : rssItems) {
        Log.i("PkRSS", item.toShortString());
    }
} catch (Exception e) {
    Log.i("Error", "eror");
    t.setText("RSSerror");
}

Although you may want to load these asynchronously using PkRSS.with(this).load(url).callback(this).async();

Upvotes: 4

Related Questions