java - Parsing XML with XmlPullParserFactory is crashing in while statement -
i using xmlpullparserfactory
object parse xml
receive server. reason crashes @ while
statement , can't figure out why happen.
xml:
<?xml version="1.0" encoding="utf-8"?> <body> <item> <id>115240</id> <title> <![cdata[student evangelism team leaves fiji]]> </title> <created>20130821-15:50</created> <author> <![cdata[staff writer]]> </author> <summary> <![cdata[on august 21, service, justice, , missions coordinator fabio maia , ten pacific union college students left eighteen-day evangelism mission in fiji sharehim , quiet hour ministries.]]> </summary> <body_text> <![cdata[<p>on august 21, service, justice, , missions coordinator fabio maia , ten pacific union college students left eighteen-day evangelism mission in fiji sharehim , quiet hour ministries. each student placed in specific community present story of christ’s love , sacrifice each evening. many of students active in college’s praise , worship teams , brought instruments can share god’s love through music.</p> <p>before group departed, students gathered in president heather j. knight’s office special blessing prayer led pastor mark witas, lead pastor of puc church. pastor witas included special prayer god lead group in doing in name. <a></a>the group bonded sharing meal before heading airport flight fiji.</p> <p>for many puc students, summer perfect opportunity devote time ministry , service. while students work @ summer camps or serve literature evangelists, other groups experience of lifetime sharing gospel , serving local communities in places around world. puc community invited join in prayer team travelling fiji other student groups on short-term mission trips nicaragua , brazil.</p>]]> </body_text> <url>http://www.puc.edu/news/archives/2013/student-evangelism-team-leaves-for-fiji</url> <images> <image url="http://www.puc.edu/__data/assets/image/0014/115241/fiji.jpg" url_2x="http://www.puc.edu/__data/assets/image/0014/115241/varieties/2x.jpg" thumb_small="http://www.puc.edu/__data/assets/image/0014/115241/varieties/thumb.jpg" thumb_medium="http://www.puc.edu/__data/assets/image/0014/115241/varieties/thumb_large.jpg" thumb_large="http://www.puc.edu/__data/assets/image/0014/115241/varieties/thumb_large.jpg" /> </images> </item> ... lots more item nodes </body>
java:
class pucnewsitem { public string title; public string summary; public string body; public string url; public string imageurl; } public class pucdatasource extends activity { public static arraylist<pucnewsitem> getpucnews() throws ioexception { string url = "http://api.mysite.com/list"; inputstream = downloadurl(url); xmlpullparserfactory pullparserfactory; try { pullparserfactory = xmlpullparserfactory.newinstance(); xmlpullparser parser = pullparserfactory.newpullparser(); parser.setinput(is, null); arraylist<pucnewsitem> items = null; int eventtype = parser.geteventtype(); pucnewsitem item = null; log.d("debug: ", "start"); while (eventtype != xmlpullparser.end_document){ string name = null; switch (eventtype){ case xmlpullparser.start_document: log.d("debug: ", "start doc"); items = new arraylist<pucnewsitem>(); break; case xmlpullparser.start_tag: name = parser.getname(); if (name == "item"){ item = new pucnewsitem(); } else if (item != null){ if (name == "title"){ item.title = parser.nexttext(); } else if (name == "summary"){ item.summary = parser.nexttext(); } else if (name == "body_text"){ item.body = parser.nexttext(); } } break; case xmlpullparser.end_tag: name = parser.getname(); if (name.equalsignorecase("item") && item != null) { items.add(item); } break; }//end switch eventtype = parser.next(); }//end while return items; } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; }//end }//end class
errors:
09-03 16:31:50.100: w/system.err(16727): invalid stream or encoding: java.io.ioexception: stream closed (position:start_document null@1:1) caused by: java.io.ioexception: stream closed; nested exception is: 09-03 16:31:50.108: w/system.err(16727): java.io.ioexception: stream closed 09-03 16:31:50.108: w/system.err(16727): @ java.util.zip.gzipinputstream.read(gzipinputstream.java:158) 09-03 16:31:50.108: w/system.err(16727): @ libcore.io.streams.readsinglebyte(streams.java:41) 09-03 16:31:50.108: w/system.err(16727): @ java.util.zip.inflaterinputstream.read(inflaterinputstream.java:128) 09-03 16:31:50.108: w/system.err(16727): @ org.kxml2.io.kxmlparser.setinput(kxmlparser.java:1623) 09-03 16:31:50.108: w/system.err(16727): @ com.puc.mobile.pucdatasource.getpucnews(pucdatasource.java:40) 09-03 16:31:50.108: w/system.err(16727): @ com.puc.mobile.mainactivity$downloadpucnews.doinbackground(mainactivity.java:94) 09-03 16:31:50.108: w/system.err(16727): @ com.puc.mobile.mainactivity$downloadpucnews.doinbackground(mainactivity.java:1) 09-03 16:31:50.108: w/system.err(16727): @ android.os.asynctask$2.call(asynctask.java:264) 09-03 16:31:50.108: w/system.err(16727): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 09-03 16:31:50.108: w/system.err(16727): @ java.util.concurrent.futuretask.run(futuretask.java:137) 09-03 16:31:50.108: w/system.err(16727): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:208) 09-03 16:31:50.108: w/system.err(16727): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 09-03 16:31:50.108: w/system.err(16727): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 09-03 16:31:50.108: w/system.err(16727): @ java.lang.thread.run(thread.java:856) 09-03 16:31:50.108: d/androidruntime(16727): shutting down vm 09-03 16:31:50.108: w/dalvikvm(16727): threadid=1: thread exiting uncaught exception (group=0x40aba210) 09-03 16:31:50.108: e/androidruntime(16727): fatal exception: main 09-03 16:31:50.108: e/androidruntime(16727): java.lang.nullpointerexception 09-03 16:31:50.108: e/androidruntime(16727): @ com.puc.mobile.mainactivity$downloadpucnews.onpostexecute(mainactivity.java:103) 09-03 16:31:50.108: e/androidruntime(16727): @ com.puc.mobile.mainactivity$downloadpucnews.onpostexecute(mainactivity.java:1) 09-03 16:31:50.108: e/androidruntime(16727): @ android.os.asynctask.finish(asynctask.java:602) 09-03 16:31:50.108: e/androidruntime(16727): @ android.os.asynctask.access$600(asynctask.java:156) 09-03 16:31:50.108: e/androidruntime(16727): @ android.os.asynctask$internalhandler.handlemessage(asynctask.java:615) 09-03 16:31:50.108: e/androidruntime(16727): @ android.os.handler.dispatchmessage(handler.java:99) 09-03 16:31:50.108: e/androidruntime(16727): @ android.os.looper.loop(looper.java:137) 09-03 16:31:50.108: e/androidruntime(16727): @ android.app.activitythread.main(activitythread.java:4697) 09-03 16:31:50.108: e/androidruntime(16727): @ java.lang.reflect.method.invokenative(native method) 09-03 16:31:50.108: e/androidruntime(16727): @ java.lang.reflect.method.invoke(method.java:511) 09-03 16:31:50.108: e/androidruntime(16727): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:787) 09-03 16:31:50.108: e/androidruntime(16727): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:554) 09-03 16:31:50.108: e/androidruntime(16727): @ dalvik.system.nativestart.main(native method)
any ideas on why crash happening? should somehow specifying name of roote node?
the problem doesn't appear while loop, according stack trace.
09-03 16:31:50.108: w/system.err(16727): java.io.ioexception: stream closed 09-03 16:31:50.108: w/system.err(16727): @ java.util.zip.gzipinputstream.read(gzipinputstream.java:158) 09-03 16:31:50.108: w/system.err(16727): @ libcore.io.streams.readsinglebyte(streams.java:41) 09-03 16:31:50.108: w/system.err(16727): @ java.util.zip.inflaterinputstream.read(inflaterinputstream.java:128) 09-03 16:31:50.108: w/system.err(16727): @ org.kxml2.io.kxmlparser.setinput(kxmlparser.java:1623)
it's throwing exception @ setinput() in code, before while loop.
the root cause here lot of things. i'd start looking @ downloadurl() code, , see if there's reason why might returning stream that's closed.
Comments
Post a Comment