I try to parse XML document and add the result to list. I'm getting null every time. I have no idea what i am doing wrong. The reading is outside the main Thread, and at the end it call mathod that send Intent to another Activity. You can look at the XML from the URL in the code. Thanks for answering. This is my code:
t = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(
"http://ift.tt/1u4CwMq");
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();
No comments:
Post a Comment