【发布时间】:2015-02-04 00:41:17
【问题描述】:
我尝试解析 XML 文档并将结果添加到列表中。 我每次都为空。我不知道我做错了什么。 读取在主线程之外,最后它调用将 Intent 发送到另一个 Activity 的方法。 您可以从代码中的 URL 查看 XML。 谢谢回答。 这是我的代码:
t = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(
"http://www.ynet.co.il/Integration/StoryRss4880.xml");
XmlPullParserFactory factory = XmlPullParserFactory
.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
int eventType = xpp.getEventType();
String l = null;
String tit = null;
String p = null;
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
l = xpp.nextText();
}
} else if (xpp.getName().equalsIgnoreCase(
"link")) {
if (insideItem) {
tit = xpp.nextText();
}
} else if (xpp.getName().equalsIgnoreCase(
"description")) {
if (insideItem) {
String array[] = xpp.nextText().split(
"img src='");
String array1[] = array[1]
.split("' alt");
p = array1[0];
}
}
MyArticle.getInstance().getmArticle()
.add(new Article(l, tit, p));
}
} else if (eventType == XmlPullParser.END_TAG
&& xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
eventType = xpp.next();
}
if (eventType == XmlPullParser.END_DOCUMENT) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
GoTOActivity(context);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
t.start();
【问题讨论】:
-
哪里抛出异常,贴stacktrace。
-
也不例外。该列表保持为空...