Showing posts with label rss. Show all posts
Showing posts with label rss. Show all posts

Monday, July 13, 2009

Problem reading Google RSS from Java

I dont know why the google makes it a problem. But when you try to access a RSS feed posted by google from java application it gives the following error.

java.io.IOException: Server returned HTTP response code: 403 for URL:

Here im using Rome to read the RSS feeds made by a google group. Here other than passing the url to XmlReader, im passing a URLConnection. But the difference is, here im faking this request as a request made by a Mozilla browser, by setting up the User-agent property.

urlConn.setRequestProperty("User-agent","Mozilla/2.0.0.11");
reader = new XmlReader(urlConn);
SyndFeedInput input = new SyndFeedInput();
SyndFeed feed = input.build(reader); 

And i guess you cant make more than 1000 requests for the google per day. So dont make any applications which exceed that limit.

Enjoy !!

Read RSS using ROME ( sample code )

ROME is an open source tool to parse, generate and publish RSS and Atom feeds. Using Rome you can parse the available RSS and Atom feeds. Without bothering about format and version of RSS feed. The core library depends on the JDOM XML parser.
Considering you have all the required jar files we will start with reading the RSS feed. ROME represents syndication feeds (RSS and Atom) as instances of the com.sun.syndication.synd.SyndFeed interface. This library can be downloaded from here.

ROME includes parsers to process syndication feeds into SyndFeed instances. The SyndFeedInput class handles the parsers using the correct one based on the syndication feed being processed. The developer does not need to worry about selecting the right parser for a syndication feed, the SyndFeedInput will take care of it by peeking at the syndication feed structure.
A sample java code can be described as follows:
package com.infosys.hanumant.rome;

import java.net.URL;
import java.util.Iterator;

import com.sun.syndication.feed.synd.SyndEntry;
import com.sun.syndication.feed.synd.SyndFeed;
import com.sun.syndication.io.SyndFeedInput;
import com.sun.syndication.io.XmlReader;

/**
* @author Hanumant Shikhare
*/
public class Reader {

public static void main(String[] args) throws Exception {

URL url = new URL("http://viralpatel.net/blogs/feed");
XmlReader reader = null;

try {

reader = new XmlReader(url);
SyndFeed feed = new SyndFeedInput().build(reader);
System.out.println("Feed Title: "+ feed.getAuthor());

for (Iterator i = feed.getEntries().iterator(); i.hasNext();) {
SyndEntry entry = (SyndEntry) i.next();
System.out.println(entry.getTitle());
}
} finally {
if (reader != null)
reader.close();
}
}
}