package com.weather.business;
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.system.ApplicationManager;
import net.rim.device.api.xml.parsers.SAXParser;
import net.rim.device.api.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.*;
public class FeedReader extends Thread {
final Feed feed;
public FeedReader()
{
this.feed = new Feed("http://weather.uwaterloo.ca/waterloo_weather_station_headlines.xml","http://weather.uwaterloo.ca/waterloo_weather_station_headlines.xml");
}
public void run ()
{
InputStream inputStrem = null;
HttpConnection httpConnection = null;
try
{
httpConnection = (HttpConnection) Connector.open(feed.getUrl());
inputStrem = httpConnection.openDataInputStream();
//finds if connection is good
if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK)
{
String desiredEncoding = "ISO-8859-1"; //iso-8859-1
String contenttype = httpConnection.getHeaderField("Content-Type");
if(contenttype != null)
{
contenttype = contenttype.toUpperCase();
if (contenttype.indexOf("UTF-8") != -1)
{
desiredEncoding = "UTF-8";
}
InputSource is = new InputSource(inputStrem);
is.setEncoding(desiredEncoding);
// create the factory
SAXParserFactory factory = SAXParserFactory.newInstance();
// create a parser
SAXParser parser = factory.newSAXParser();
XMLHandler xmlHander = new XMLHandler(feed);
parser.parse(is, xmlHander);
}
}
}
catch (Exception err)
{
System.out.println(err.getMessage());
}
}
}
class XMLHandler extends DefaultHandler
{
StringBuffer sb = null;
Feed _feed = null;
Item item = null;
boolean bStarted = false;
XMLHandler(Feed feed)
{
_feed = feed;
}
public void warning(SAXParseException e)
{
System.err.println("warning: " + e.getMessage());
bStarted = false;
}
public void error(SAXParseException e)
{
System.err.println("error: " + e.getMessage());
}
public void fatalError(SAXParseException e)
{
System.err.println("fatalError: " + e.getMessage());
bStarted = false;
}
public void startDocument() throws SAXException
{
}
public void endDocument() throws SAXException
{
//ended
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException
{
sb = new StringBuffer("");
if (localName.equals("item"))
{
bStarted = true;
// new item, let's set up!
item = new Item();
}
}
public void endElement(String namespaceURI, String localName,
String qName) throws SAXException
{
System.out.println("");
System.out.println("endElement>> localName: "+localName+" sb: "+sb.toString());
System.out.println("");
if (bStarted == false) return;
if (localName.equals("item"))
{
item.setTitle(_feed.getName());
}
if (localName.equals("title"))
{
item.setTitle(sb.toString());
}
if (localName.equals("link"))
{
item.setLink(sb.toString());
}
if (localName.equals("description"))
{
item.setDescription(sb.toString());
}
if (localName.equals("category"))
{
item.setCategory(sb.toString());
}
if (localName.equals("pubDate"))
{
item.setPubDate(sb.toString());
}
sb = new StringBuffer("");
}
public void characters(char ch[], int start, int length)
{
String theString = new String(ch,start,length);
sb.append(theString);
System.out.println("characters: "+theString);
}
}
class Feed
{
final String feed_name;
final String feed_url;
public Feed(String feed_name, String feed_url)
{
this.feed_name = feed_name;
this.feed_url = feed_url;
}
public String getName() {
// TODO Auto-generated method stub
return feed_name;
}
public String getUrl()
{
return feed_url;
}
}
class Item
{
String pubDate;
String title;
String link;
String description;
String category;
public void setPubDate(String _pubDate) {
pubDate= _pubDate;
print(pubDate);
}
public void setTitle(String _title) {
title= _title;
print(title);
}
public void setLink(String _link) {
link = _link;
print(link);
}
public void setDescription(String _description) {
description = _description;
print(category);
}
public void setCategory(String _category) {
category = _category;
print(category);
}
private void print(String _print)
{
System.out.println(_print);
}
}
Thursday, April 16, 2009
Blackbrry RSS feeds
With one Http connection and XML reader, it is pretty easy to make a rss application.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment