android - Parse xml from url. Exception -
i need info url("http://someurl.com/file.xml"). info in tags - "name", "surname".
how can ?
i know must using asynctask internet connection.
i trying using xmlpullparser in asynctask, exception "indeoutofbound"
class requesttask extends asynctask<string, void, void> { string name; @override protected inputstream doinbackground(string... uri) { try { xmlpullparserfactory factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); xmlpullparser xpp = factory.newpullparser(); xpp.setinput(new inputstreamreader( geturldata("http://someurl.com/file.xml"))); while (xpp.geteventtype() != xmlpullparser.end_document) { if (xpp.geteventtype() == xmlpullparser.start_tag) { if (xpp.getname().equals("name")) { name=xpp.getattributevalue(0); } } xpp.next(); } } catch (throwable t) { } } catch (ioexception e) { //handle problems.. } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); toast.maketext(getactivity().getapplicationcontext(), "name="+name, toast.length_short).show(); }
}
look @ xml in "file.xml".
i suspect error here:
... if (xpp.geteventtype() == xmlpullparser.start_tag) { if (xpp.getname().equals("name")) { name=xpp.getattributevalue(0); // <- your"<name>" tag have attributes???
here tutorials on android xml processing:
- http://www.javatpoint.com/android-xmlpullparser-tutorial
- http://www.xmlpull.org/v1/download/unpacked/doc/quick_intro.html
- http://www.vogella.com/tutorials/androidxml/article.html
in particular, note use of gettext()
fetch element content.
Comments
Post a Comment