📄 rssnewsmidlet.java
字号:
/*
* RSSNewsMIDlet.java
*
* Created on July 18, 2007, 3:13 PM
*/
package org.secangkirkopipanas.rss;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author lilik.haryanto
*/
public class RSSNewsMIDlet extends MIDlet implements CommandListener {
// private String[] sourceLinkList = {
// "http://www.nytimes.com/services/xml/rss/nyt/National.xml"
// };
private String[] sourceLinkList = {
"http://localhost:8081/news/NYTNational.xml"
};
private int selectedIndex = 0;
private HttpConnection conn;
private DataInputStream dis;
private DataOutputStream dos;
private RSSParser parser;
/** Creates a new instance of RSSNewsMIDlet */
public RSSNewsMIDlet() {
initialize();
}
private List listChannel;//GEN-BEGIN:MVDFields
private List listNews;
private Form formDescription;
private Command okCommand;
private Command backCommandDesc;
private Command exitCommand;
private Command backCommandNews;
private Alert alertError;//GEN-END:MVDFields
//GEN-LINE:MVDMethods
/** Called by the system to indicate that a command has been invoked on a particular displayable.//GEN-BEGIN:MVDCABegin
* @param command the Command that ws invoked
* @param displayable the Displayable on which the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {//GEN-END:MVDCABegin
// Insert global pre-action code here
if (displayable == listChannel) {//GEN-BEGIN:MVDCABody
if (command == listChannel.SELECT_COMMAND) {
switch (get_listChannel().getSelectedIndex()) {
case 0://GEN-END:MVDCABody
selectedIndex = get_listChannel().getSelectedIndex();
try {
conn = (HttpConnection) Connector.open(sourceLinkList[selectedIndex]);
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("Content-Type", "text/xml");
dos = new DataOutputStream(conn.openDataOutputStream());
parser = new RSSParser(50);
parser.parse(new DataInputStream(conn.openDataInputStream()));
getDisplay().setCurrent(get_listNews());//GEN-LINE:MVDCAAction13
int count = parser.getCount();
for (int i = 0; i < count; i++) {
get_listNews().append(
parser.getRss().getItems()[i].getTitle() + "\n" +
parser.getRss().getItems()[i].getPubDate(), null);
}
} catch (Exception e) {
get_alertError().setString(e.getMessage());
getDisplay().setCurrent(get_alertError());
}
break;//GEN-BEGIN:MVDCACase13
}
} else if (command == exitCommand) {//GEN-END:MVDCACase13
// Insert pre-action code here
exitMIDlet();//GEN-LINE:MVDCAAction32
// Insert post-action code here
}//GEN-BEGIN:MVDCACase32
} else if (displayable == listNews) {
if (command == okCommand) {//GEN-END:MVDCACase32
// Insert pre-action code here
get_formDescription().deleteAll();
get_formDescription().setTitle(parser.getRss().getItems()[get_listNews().getSelectedIndex()].getTitle());
get_formDescription().append("Publish Date: " + parser.getRss().getItems()[get_listNews().getSelectedIndex()].getPubDate() + "\n" +
parser.getRss().getItems()[get_listNews().getSelectedIndex()].getDescription());
getDisplay().setCurrent(get_formDescription());//GEN-LINE:MVDCAAction26
// Insert post-action code here
} else if (command == backCommandNews) {//GEN-LINE:MVDCACase26
// Insert pre-action code here
getDisplay().setCurrent(get_listChannel());//GEN-LINE:MVDCAAction34
// Insert post-action code here
}//GEN-BEGIN:MVDCACase34
} else if (displayable == formDescription) {
if (command == backCommandDesc) {//GEN-END:MVDCACase34
// Insert pre-action code here
getDisplay().setCurrent(get_listNews());//GEN-LINE:MVDCAAction29
// Insert post-action code here
}//GEN-BEGIN:MVDCACase29
}//GEN-END:MVDCACase29
// Insert global post-action code here
}//GEN-LINE:MVDCAEnd
/** This method initializes UI of the application.//GEN-BEGIN:MVDInitBegin
*/
private void initialize() {//GEN-END:MVDInitBegin
// Insert pre-init code here
getDisplay().setCurrent(get_listChannel());//GEN-LINE:MVDInitInit
// Insert post-init code here
}//GEN-LINE:MVDInitEnd
/**
* This method should return an instance of the display.
*/
public Display getDisplay() {//GEN-FIRST:MVDGetDisplay
return Display.getDisplay(this);
}//GEN-LAST:MVDGetDisplay
/**
* This method should exit the midlet.
*/
public void exitMIDlet() {//GEN-FIRST:MVDExitMidlet
getDisplay().setCurrent(null);
destroyApp(true);
notifyDestroyed();
}//GEN-LAST:MVDExitMidlet
/** This method returns instance for listChannel component and should be called instead of accessing listChannel field directly.//GEN-BEGIN:MVDGetBegin2
* @return Instance for listChannel component
*/
public List get_listChannel() {
if (listChannel == null) {//GEN-END:MVDGetBegin2
// Insert pre-init code here
listChannel = new List("The Channels:", Choice.IMPLICIT, new String[] {"US News"}, new Image[] {null});//GEN-BEGIN:MVDGetInit2
listChannel.addCommand(get_exitCommand());
listChannel.setCommandListener(this);
listChannel.setSelectedFlags(new boolean[] {false});//GEN-END:MVDGetInit2
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd2
return listChannel;
}//GEN-END:MVDGetEnd2
/** This method returns instance for listNews component and should be called instead of accessing listNews field directly.//GEN-BEGIN:MVDGetBegin6
* @return Instance for listNews component
*/
public List get_listNews() {
if (listNews == null) {//GEN-END:MVDGetBegin6
// Insert pre-init code here
listNews = new List("List of News", Choice.IMPLICIT, new String[0], new Image[0]);//GEN-BEGIN:MVDGetInit6
listNews.addCommand(get_okCommand());
listNews.addCommand(get_backCommandNews());
listNews.setCommandListener(this);
listNews.setSelectedFlags(new boolean[0]);//GEN-END:MVDGetInit6
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd6
return listNews;
}//GEN-END:MVDGetEnd6
/** This method returns instance for formDescription component and should be called instead of accessing formDescription field directly.//GEN-BEGIN:MVDGetBegin23
* @return Instance for formDescription component
*/
public Form get_formDescription() {
if (formDescription == null) {//GEN-END:MVDGetBegin23
// Insert pre-init code here
formDescription = new Form(null, new Item[0]);//GEN-BEGIN:MVDGetInit23
formDescription.addCommand(get_backCommandDesc());
formDescription.setCommandListener(this);//GEN-END:MVDGetInit23
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd23
return formDescription;
}//GEN-END:MVDGetEnd23
/** This method returns instance for okCommand component and should be called instead of accessing okCommand field directly.//GEN-BEGIN:MVDGetBegin25
* @return Instance for okCommand component
*/
public Command get_okCommand() {
if (okCommand == null) {//GEN-END:MVDGetBegin25
// Insert pre-init code here
okCommand = new Command("Ok", Command.OK, 1);//GEN-LINE:MVDGetInit25
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd25
return okCommand;
}//GEN-END:MVDGetEnd25
/** This method returns instance for backCommandDesc component and should be called instead of accessing backCommandDesc field directly.//GEN-BEGIN:MVDGetBegin28
* @return Instance for backCommandDesc component
*/
public Command get_backCommandDesc() {
if (backCommandDesc == null) {//GEN-END:MVDGetBegin28
// Insert pre-init code here
backCommandDesc = new Command("Back", Command.BACK, 1);//GEN-LINE:MVDGetInit28
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd28
return backCommandDesc;
}//GEN-END:MVDGetEnd28
/** This method returns instance for exitCommand component and should be called instead of accessing exitCommand field directly.//GEN-BEGIN:MVDGetBegin31
* @return Instance for exitCommand component
*/
public Command get_exitCommand() {
if (exitCommand == null) {//GEN-END:MVDGetBegin31
// Insert pre-init code here
exitCommand = new Command("Exit", Command.EXIT, 1);//GEN-LINE:MVDGetInit31
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd31
return exitCommand;
}//GEN-END:MVDGetEnd31
/** This method returns instance for backCommandNews component and should be called instead of accessing backCommandNews field directly.//GEN-BEGIN:MVDGetBegin33
* @return Instance for backCommandNews component
*/
public Command get_backCommandNews() {
if (backCommandNews == null) {//GEN-END:MVDGetBegin33
// Insert pre-init code here
backCommandNews = new Command("Back", Command.BACK, 1);//GEN-LINE:MVDGetInit33
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd33
return backCommandNews;
}//GEN-END:MVDGetEnd33
/** This method returns instance for alertError component and should be called instead of accessing alertError field directly.//GEN-BEGIN:MVDGetBegin35
* @return Instance for alertError component
*/
public Alert get_alertError() {
if (alertError == null) {//GEN-END:MVDGetBegin35
// Insert pre-init code here
alertError = new Alert("Error Found", "<Enter Text>", null, null);//GEN-BEGIN:MVDGetInit35
alertError.setTimeout(-2);//GEN-END:MVDGetInit35
// Insert post-init code here
}//GEN-BEGIN:MVDGetEnd35
return alertError;
}//GEN-END:MVDGetEnd35
public void startApp() {
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -