我正在为Android构建一个RSS阅读器,但我不知道我做错了什么。 我使用了本教程:http://developer.android.com/training/basics/network-ops/xml.html
我总是收到错误:
/ RSSXMLHELPER ::错误:org.xmlpull.v1.XmlPullParserException:expected:START_TAG {null} rss(位置:START_DOCUMENT null @ 1:1 in java.io.InputStreamReader@12c8977c)
这是我的XMLPullParser类:
public class RSSXMLHelper {
PostData postData;
ArrayList<PostData> pList;
public ArrayList<PostData> parse(InputStream in) throws XmlPullParserException, IOException {
if (in != null) {
try {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in, null);
parser.nextTag();
return readRss(parser);
} finally {
if (in != null) {
in.close();
}
}
} else {
postData = new PostData();
pList = new ArrayList<>();
//l.add(entry);
return pList;
}
}
private ArrayList<PostData> readRss(XmlPullParser parser)
throws XmlPullParserException, IOException {
ArrayList<PostData> items = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "rss");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("channel")) {
items.addAll(readChannel(parser));
} else {
skip(parser);
}
}
return items;
}
private ArrayList<PostData> readChannel(XmlPullParser parser)
throws IOException, XmlPullParserException {
ArrayList<PostData> items = new ArrayList<>();
parser.require(XmlPullParser.START_TAG, null, "channel");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("item")) {
items.add(readItem(parser));
} else {
skip(parser);
}
}
return items;
}
private PostData readItem(XmlPullParser parser) throws XmlPullParserException, IOException {
PostData postData = new PostData();
parser.require(XmlPullParser.START_TAG, null, "item");
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
if (name.equals("title")) {
postData.setPostTitle(readTitle(parser));
} else {
skip(parser);
}
}
return postData;
}
private String readTitle(XmlPullParser parser)
throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, null, "title");
String title = readText(parser);
parser.require(XmlPullParser.END_TAG, null, "title");
return title;
}
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;
}
}
}
}
我正在搜索错误2小时,但我真的不知道我的代码有什么问题....
编辑:
XML文件:ifrick.ch/feed
答案 0 :(得分:0)
我明白了。代码是对的。
问题在于我正在编写一个新类,并将其放在现有类后面进行测试。当涉及新的XMLReader类时,Inputstream已经关闭......