我编写了这个函数来解析URL中的xml https://gdata.youtube.com/feeds/api/users/yrf/uploads
void getvideodetail() throws Exception {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
String url = "https://gdata.youtube.com/feeds/api/users/yrf/uploads";
URL input = new URL(url);
xpp.setInput(input.openStream(), null);
int eventType = xpp.getEventType();
boolean entry_flag = false;
boolean group_flag = false;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equals("entry") || entry_flag) {
entry_flag = true;
if (xpp.getName().equals("id")) {
Log.i("temp", xpp.nextText());
try {
if (xpp.nextText() != null) {
String temp = xpp.nextText();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else if (eventType == XmlPullParser.END_TAG) {
if (xpp.getName().equals("entry")) {
entry_flag = false;
}
}
eventType = xpp.next();
}
}
从这个解析我需要视频ID,它们在标签中。问题是我能够在LogCat中看到来自id标签的文本,但无法在字符串中获取文本。我得到了这个例外。
10-26 14:18:18.883: WARN/System.err(329): org.xmlpull.v1.XmlPullParserException: precondition: START_TAG (position:END_TAG </id>@201:1770 in java.io.InputStreamReader@40642130)
10-26 14:18:18.883: WARN/System.err(329): at org.kxml2.io.KXmlParser.exception(KXmlParser.java:273)
10-26 14:18:18.913: WARN/System.err(329): at org.kxml2.io.KXmlParser.nextText(KXmlParser.java:1430)
10-26 14:18:18.913: WARN/System.err(329): at com.galerywala.Gallerywala2Activity.getvideodetail(Gallerywala2Activity.java:309)
10-26 14:18:18.913: WARN/System.err(329): at com.galerywala.Gallerywala2Activity.get_tv_data(Gallerywala2Activity.java:257)
10-26 14:18:18.933: WARN/System.err(329): at com.galerywala.Gallerywala2Activity$4.run(Gallerywala2Activity.java:222)
10-26 14:18:18.933: WARN/System.err(329): at java.lang.Thread.run(Thread.java:1019)
有人请为这个问题提供一些解决方案。
由于