NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

J2ME MIDP RSS Reader application

 
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users
View previous topic :: View next topic  
Author Message
Ivy



Joined: 14 Mar 2009
Posts: 20
Location: Philippines

PostPosted: Mon Sep 14, 2009 3:45 am    Post subject: J2ME MIDP RSS Reader application Reply with quote

This is the list of codes that has a problem. I dont know what is the cause of the problem. I have followed all the steps and has added source files for xml pull.. but it seems that xmlpull has an error because it says that org.xmlpull.v1 does not exist. what wrong with this one?

package RssReader;

import javax.microedition.io.*;
import java.util.*;
import java.io.*;
import org.xmlpull.v1.*;
import org.kxml2.io.*;

/**
* RssFeedParser is an utility class for aquiring and parsing a RSS feed.
* HttpConnection is used to fetch RSS feed and kXML is used on xml parsing.
*/
public class RssFeedParser {

private RssFeed m_rssFeed; // The RSS feed
private XmlPullParser m_xmlParser = new KXmlParser(); // The Xml parser

/** Create new instance of RssDocument */
public RssFeedParser(RssFeed rssFeed) {
m_rssFeed = rssFeed;
}

/** Return RSS feed */
public RssFeed getRssFeed() {
return m_rssFeed;
}

/** send a GET request to web server */
public void parseRssFeed()
throws IOException, Exception {

HttpConnection hc = null;
DataInputStream dis = null;
String response = "";
try {
/**
* Open an HttpConnection with the Web server
* The default request method is GET
*/
hc = (HttpConnection) Connector.open( m_rssFeed.getUrl() );
hc.setRequestMethod(HttpConnection.GET);
/** Some web servers requires these properties */
hc.setRequestProperty("User-Agent",
"Profile/MIDP-1.0 Configuration/CLDC-1.0");
hc.setRequestProperty("Content-Length", "0");
hc.setRequestProperty("Connection", "close");

/**
* Get a DataInputStream from the HttpConnection
* and forward it to kXML parser
*/
parseRssFeedXml( hc.openInputStream() );
} catch(Exception e) {
throw new Exception("Error while parsing RSS data: "
+ e.toString());
} finally {
if (hc != null) hc.close();
if (dis != null) dis.close();
}
}

/**
* Nasty RSS feed XML parser.
* Seams to work with both RSS 1.0 and 2.0.
*/
public void parseRssFeedXml(InputStream is)
throws IOException, XmlPullParserException {
/** Initialize item collection */
m_rssFeed.getItems().removeAllElements();

/** Initialize XML parser and parse RSS XML */
KXmlParser parser = new KXmlParser();
parser.setInput( new InputStreamReader(is) );

/** RSS item properties */
String title = null;
String description = null;
String link = null;

/** <?xml...*/
parser.nextTag();

/** Various tags... Wait for the <item> tag */
parser.require(parser.START_TAG, null, null);
while(!"item".equals(parser.getName()) ){
/** Check if document doesn't include any item tags */
if( parser.next() == parser.END_DOCUMENT )
throw new IOException("No items in RSS feed!");
}

/** Parse <item> tags */
do {
parser.require(parser.START_TAG, null, null);

/** Initialize properties */
title = "";
description = "";
link = "";

/** One <item> tag handling*/
while (parser.nextTag() != parser.END_TAG) {
parser.require(parser.START_TAG, null, null);
String name = parser.getName();
String text = parser.nextText();

/** Save item property values */
if (name.equals("title"))
title = text;
else if (name.equals("description"))
description = text;
else if (name.equals("link"))
link = text;

parser.require(parser.END_TAG, null, name);
}

/** Debugging information */
System.out.println ("Title: " + title);
System.out.println ("Link: " + link);
System.out.println ("Description: " + description);

/** Create new RSS item and add it do RSS document's item
* collection
*/
RssItem rssItem = new RssItem(title, link, description);
m_rssFeed.getItems().addElement( rssItem );
parser.nextTag();

} while("item".equals(parser.getName()));
}
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> Java ME Users All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo