我正在创建一个小应用程序,以将 BBC f1 新闻提要读入 android 并以ListView
. 我按照这个Android Developer Tutorial创建了以下解析函数。
当items ArrayList
返回到活动时,它只有一项,即最近的新闻报道。当我登录
在通过异步调用成功检索数据后调用这些函数。在倒数第二行,输出标题与返回的数组项相同。
我已在此处将 logcat 输出发布到 pastebin
RSS 结构
<rss xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
// unrequired tags (title, description, lang etc)
<item>
<title></title>
<description></description>
<link></link>
<guid isPermaLink="false"></guid>
<pubDate></pubDate>
<media:thumbnail />
<media:thumbnail />
</item>
<item>
<title></title>
<description></description>
<link></link>
<guid isPermaLink="false"></guid>
<pubDate></pubDate>
<media:thumbnail />
<media:thumbnail />
</item>
</channel>
</rss>
读取提要功能
private static ArrayList<BBCItem> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
//Log.d("read feed", "");
ArrayList<BBCItem> items = new ArrayList();
parser.require(XmlPullParser.START_TAG, ns, "rss");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
// Starts by looking for the channel tag
if (name.equals("channel")) {
// navigate to the item tag here
parser.require(XmlPullParser.START_TAG, ns, "channel");
while (parser.next() != XmlPullParser.END_TAG){
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
name = parser.getName();
if (name.equals("item")){
items.add(bbcReadEntry(parser));
}
else {
skip(parser);
}
}
} else {
skip(parser);
}
}
return items;
}
读取单个提要项
public static BBCItem bbcReadEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "item");
String title = null;
String description = null;
String url = null;
String pubDate = null;
String thumbnailURL = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("title")) {
title = readString(parser, "title");
} else if (name.equals("description")) {
description = readString(parser, "description");
} else if (name.equals("link")) {
url = readString(parser, "link");
} else if (name.equals("pubDate")) {
pubDate = readString(parser, "pubDate");
} else if (name.equals("media:thumbnail")) {
//thumbnailURL = readThumbnail(parser);
thumbnailURL = "http://news.bbcimg.co.uk/media/images/81615000/jpg/_81615988_nicorosberg.jpg";
}
else {
skip(parser);
}
}
Log.d("News item title", title);
return new BBCItem(title, description, url, pubDate, thumbnailURL);
}
非常感谢任何帮助
谢谢