📄 rssparser.java
字号:
/*
* RSSParser.java
*
* Created on 2006年12月20日, 上午2:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package cn.edu.uestc.pandarss;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import org.xmlpull.v1.*;
import org.kxml2.io.*;
import java.io.*;
/**
*
* @author pandaxiaoxi
*/
/*--------------------------------------------------
* NewsParser.java
*
* 解析RSS Feed源
*-------------------------------------------------*/
public class RSSParser implements Runnable {
private String source; // News source to read from
private InfoForm fmInfo; // List component of headlines
private TitleList listComponent; // List component of headlines
private RSSReader midlet;
public RSSParser(RSSReader midlet, String source, InfoForm fmInfo,TitleList listComponent) {
this.midlet = midlet;
this.source = source;
this.fmInfo = fmInfo;
this.listComponent = listComponent;
}
public void run() {
parseRSSFeed();
}
/*--------------------------------------------------
* 启动线程
*-------------------------------------------------*/
public void start() {
Thread thread = new Thread(this);
try {
thread.start();
} catch (Exception e) {
fmInfo.setMessage("Fatal: Unable to start the NewsReader.");
}
}
/*--------------------------------------------------
* 解析RSS Feed源
*-------------------------------------------------*/
private void parseRSSFeed() {
HttpConnection conn = null;
try {
fmInfo.setMessage("trying to connect ...");
conn = (HttpConnection) Connector.open(source);
if (conn == null) {
fmInfo.setMessage("no connection to \n" + source);
return;
} else {
fmInfo.setMessage("reading ...");
}
InputStream is = conn.openInputStream();
fmInfo.setMessage("parsing ...");
KXmlParser parser = new KXmlParser();
// 设置null让KXmlParser自动检测该使用哪种编码
parser.setInput( is, null);
fmInfo.setMessage("done parsing ...");
parser.nextTag();
int event = parser.getEventType();
String headlineTitle = null, headlineDescription = null;
fmInfo.setMessage("building news list ...");
midlet.display.setCurrent(listComponent);
while (true) {
if (event == XmlPullParser.START_TAG && parser.getName().equals("item")) {
// get more content
event = parser.next();
// loop through the "item", obtaining the title, link and description
while (true) {
if (event == XmlPullParser.START_TAG && parser.getName().equals("title")) {
parser.next();
//System.out.println("TITLE = " + parser.getText());
headlineTitle = parser.getText().trim();
} else if (event == XmlPullParser.START_TAG && parser.getName().equals("description")) {
parser.next();
headlineDescription = parser.getText().trim();
}
event = parser.next();
if (event == XmlPullParser.END_TAG && parser.getName().equals("item") == true) {
// Send data to Headline list component
listComponent.newTitle(headlineTitle, headlineDescription);
break;
}
}
}
if (event == XmlPullParser.END_TAG && parser.getName().equals("rss")) {
break;
}
event = parser.next();
}
} catch (Exception e) {
fmInfo.setMessage("Error parsing XML!" + e.toString());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (IOException ignored) {
fmInfo.setMessage("IO exception" + ignored.toString());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -