This article Contains the Code to read an RssFile in j2me.After execution ,You can able to display the Title and an Image on Nokia Screen
The Midlet Class:
NewsListCellRenderer :
You can Create this class by Referring This URL(Just Copy the ContactsRenderer Class File)
Here is the Sample:
Thanks.........................
The Midlet Class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.sun.lwuit.*;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import java.util.Vector;
import javax.microedition.midlet.*;
/**
* @author pavan
*/
public class RssMidlet extends MIDlet implements ActionListener {
private List rssFeedList;
private Vector rssFeed;
private Image image;
private Form form1;
private Command cmdExit, cmdDetails;
//Constructor
public RssMidlet() {
Display.init(this);
rssFeed = new Vector();
cmdExit = new Command("Exit");
cmdDetails = new Command("Details");
form1 = new Form();
form1.addCommand(cmdDetails);
form1.addCommand(cmdExit);
form1.setFocus(true);
form1.addCommandListener(this);
form1.setScrollableY(true);
form1.setTransitionInAnimator(Transition3D.createRotation(250, true));
rssFeedList = new List(rssFeed);
rssFeedList.setRenderer(new NewsListCellRenderer());
rssFeedList.setFixedSelection(List.FIXED_NONE);
rssFeedList.setItemGap(0);
form1.addComponent(rssFeedList);
}
public void startApp() {
String url = "rss URL";
ParseThread myThread = new ParseThread(this);
//this will start the second thread
myThread.getXMLFeed(url);
}
public void pauseApp() {public void destroyApp(boolean unconditional) {
}
}public void DisplayError(Exception error) {
Dialog validDialog = new Dialog("Alert");
validDialog.setScrollable(false);
validDialog.setScrollable(false);
// set timeout milliseconds
validDialog.setTimeout(5000);
//pass the alert text here
TextArea textArea = new TextArea(error.getMessage());
textArea.setFocusable(false);
textArea.setScrollVisible(false);
validDialog.addComponent(textArea);
validDialog.show(0, 100, 10, 10, true);
}
public void addNews(RssModel newsItem) {
rssFeed.addElement(newsItem);
//to handle actions on the list we need to set the actionListener
rssFeedList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
List source = (List) ae.getSource();
}
});
form1.show();
}
public void actionPerformed(ActionEvent ae) {
if (ae.getCommand() == cmdExit) {
destroyApp(true);
notifyDestroyed();
}
}
}
A Model Class:
import com.sun.lwuit.Image;RssParsing Class:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author pavan
*/
public class RssModel {
private String title;
private Image image;
public RssModel(String title){
this.title=title;
// this.image=image;
}
public String getTitle(){
return title;
}
/* public Image getImage(){
return image;
}
* */
}
import com.sun.lwuit.Image;Finally
import org.kxml2.io.*;
import java.io.*;
import javax.microedition.io.*;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParser;
public class ParseThread {
protected RssMidlet parentMidlet;
private Image image;
private String title;
public ParseThread(RssMidlet parent) {
parentMidlet = parent;
}
public void getXMLFeed(final String url) {
Thread t = new Thread() {
public void run() {
HttpConnection myConnection = null;
try {
myConnection = (HttpConnection) Connector.open(url);
InputStream stream = myConnection.openInputStream();
ParseXMLFeed(stream);
} catch (Exception error) {
parentMidlet.DisplayError(error);
} finally {
try {
if (myConnection != null) {
myConnection.close();
}
} catch (IOException eroareInchidere) {
parentMidlet.DisplayError(eroareInchidere);
}
}
}
};
t.start();
}
private void ParseXMLFeed(InputStream input)
throws IOException, XmlPullParserException {
InputStreamReader isr = new InputStreamReader(input);
KXmlParser myParser = null;
try {
myParser = new KXmlParser();
myParser.setInput(isr);
myParser.nextTag();
System.out.println("nextTag");
myParser.require(XmlPullParser.START_TAG, null, "rss");
myParser.nextTag();
myParser.require(XmlPullParser.START_TAG, null, "channel");
myParser.nextTag();
myParser.require(XmlPullParser.START_TAG, null, "title");
} catch (Exception e) {
}
while (myParser.getEventType() != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
if (name.equals("channel")) {
break;
}
if (name.equals("item")) {
if (myParser.getEventType() != XmlPullParser.END_TAG) {
myParser.nextTag();
title = myParser.nextText();
//myParser.nextTag();
// String link = myParser.nextText();
// myParser.nextTag();
// String imageurl = myParser.nextText();
// ImageClass ic = new ImageClass();
// image = ic.getImage(parentMidlet, imageurl.trim());
RssModel model = new RssModel(title);
parentMidlet.addNews(model);
}
} else {
myParser.skipSubTree();
}
myParser.nextTag();
}
input.close();
}
}
NewsListCellRenderer :
You can Create this class by Referring This URL(Just Copy the ContactsRenderer Class File)
Here is the Sample:
public class NewsListCellRenderer extends Container implements ListCellRenderer {
public NewsListCellRenderer() { }
public Component getListCellRendererComponent(List list, Object value, int i, boolean bln) {}
}
Thanks.........................
No comments:
Post a Comment