我正在开发一个 RSS 阅读器应用程序,我需要在其中读取和显示部分数据。仅需要从从 url 获取的数据中提取数据,title
https ://twitter.com/statuses/user_timeline/27756405.rss。link
我在解析 xml 时遇到问题,在这个问题上需要帮助。
我的代码附在下面。
public class RssMain extends UiApplication {
RssMain theApp;
public static void main(String[] args) {
// create an instance of our app
RssMain theApp = new RssMain();
// "run" the app
theApp.enterEventDispatcher();
}
// app constructor
public RssMain() {
// create an instance of the main screen of our application
RssScreen screen = new RssScreen();
// make the screen visible
UiApplication.getUiApplication().pushScreen(screen);
}
class RssScreen extends MainScreen {
public RssScreen() {
String rssUrl = "http://twitter.com/statuses/user_timeline/27756405.rss";
String[][] urlData = RSSHandler.getURLFromRSS(rssUrl);
for (int i = 0; i < urlData.length; i++) {
String title = urlData[0][i];
String url = urlData[1][i];
System.out.println("TITLE " + title);
System.out.println("URL " + url);
}
}
}
}
RSSHandler的实现
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import net.rim.blackberry.api.browser.Browser;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.util.Arrays;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
class RSSHandler extends DefaultHandler {
boolean isItem = false;
boolean isTitle = false;
boolean isLink = false;
String[] title = new String[] {};
String[] link = new String[] {};
String value = "";
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
if (!isItem) {
if (name.equalsIgnoreCase("item"))
isItem = true;
} else {
if (name.equalsIgnoreCase("title"))
isTitle = true;
if (name.equalsIgnoreCase("link"))
isLink = true;
}
}
public void characters(char[] ch, int start, int length) throws SAXException {
if (isTitle || isLink) {
value = value.concat(new String(ch, start, length));
}
}
public void endElement(String uri, String localName, String name) throws SAXException {
if (isItem && name.equalsIgnoreCase("item")) {
isItem = false;
}
if (isTitle && name.equalsIgnoreCase("title")) {
isTitle = false;
Arrays.add(title, value);
value = "";
}
if (isLink && name.equalsIgnoreCase("link")) {
isLink = false;
Arrays.add(link, value);
value = "";
}
}
public static String[][] getURLFromRSS(String url) {
InputStream is = null;
HttpConnection connection = null;
RSSHandler rssHandler = new RSSHandler();
try {
connection = (HttpConnection) Connector.open(url);
is = connection.openInputStream();
try {
SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
parser.parse(is, rssHandler);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (connection != null)
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
String[][] result = new String[2][];
result[0] = rssHandler.title;
result[1] = rssHandler.link;
return result;
}
}