经过几个小时的搜索和调试后,我仍然被困在同一个地方,Eclipse 并没有帮助我。
我试图解析这个 RSS 提要
这很简单。连接完成,我将 InputStream 转换为字符串,这很好用。
当我尝试解析获得的字符串时,我没有错误或警告,但我尝试拥有的值为空。这是XML过程
public class XmlOperations {
private static final String ns = null;
public List parse(String in) throws XmlPullParserException, IOException {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(in));
parser.nextTag();
return readFeed(parser);
} finally {
//
}
}
private List readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
List entries = new ArrayList();
// first xml balise
parser.require(XmlPullParser.START_TAG, ns,"rss");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
Log.w(TAG, name); // output channel the first time
// Starts by looking for the item tag
if (name.equals("item")) {
**Log.w(TAG, "Never get in here" )**
entries.add(readEntry(parser));
} else {
skip(parser);
Log.w(TAG, "lala");
}
}
return entries;
}
// Parses the contents of an item. If it encounters a title, description, or link tag, hands them off
// to their respective "read" methods for processing. Otherwise, skips the tag.
private Entry readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
parser.require(XmlPullParser.START_TAG, ns, "item");
String title = null;
String description = null;
String link = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("title")) {
title = readTitle(parser);
} else if (name.equals("description")) {
description = readDescription(parser);
} else if (name.equals("link")) {
link = readLink(parser);
} else {
skip(parser);
}
}
return new Entry(title, description, link);
}
// Processes title tags in the feed.
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "title");
return title;
}
// Processes link tags in the feed.
private String readLink(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "link");
String link = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "link");
return link;
}
// Processes summary tags in the feed.
private String readDescription(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, "description");
String description = readText(parser);
parser.require(XmlPullParser.END_TAG, ns, "description");
//Log.d("vALUE = ", summary);
return description;
}
// For the tags title and description, extracts their text values.
private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
String result = "";
if (parser.next() == XmlPullParser.TEXT) {
result = parser.getText();
parser.nextTag();
}
return result;
}
private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
throw new IllegalStateException();
}
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
break;
case XmlPullParser.START_TAG:
depth++;
break;
}
}
}
我想错误出在其中一种解析方法中。
在另一个班级我有这个
XmlOperations xml = new XmlOperations();
try {
entrieses = xml.parse(result);
String lol = null;
for(Entry item : entrieses) {
//Toast.makeText(getApplicationContext(), item.title, Toast.LENGTH_SHORT).show();
lol = item.link.toString();
}
tv.setText("Pin " + lol + " gouin");
entreses 是一个条目列表。仍然等于 null :(
编辑:有了日志猫,我有频道 lala,仅此而已