xml parsing - Android Runtime exception : An error occur while executing doinbackground -
i parsing online xml using xmlpullparserfactory , showing details in listview. have put condition checking internet, takes time fetch data , crashes.the code using below.
private void runningabackprocess() { handler mhandler = new handler(looper.getmainlooper()); runnable myrunnable = new runnable() { public void run() { try{ //thread.sleep(5000); new dosometask().execute(); }catch(exception e){} // mypd_ring.dismiss(); } }; mhandler.post(myrunnable); } private class dosometask extends asynctask<void, void, void> { @override protected void doinbackground(void... arg0) { try { isinternetpresent = icd.isconnectingtointernet if(isinternetpresent){ url url = new url("njhgjghjkjhkjl"); xmlpullparserfactory factory=xmlpullparserfactory.newinstance(); xmlpullparser xpp=factory.newpullparser(); xpp.setinput(getinputstream(url), "utf_8"); int eventtype=xpp.geteventtype(); if(eventtype!=xmlpullparser.end_document) { if(eventtype==xmlpullparser.start_document) { while(eventtype!=xmlpullparser.end_document ) { xpp_name=xpp.getname(); if((xpp_name!=null) ) { -------------------- ------------------------- } } } }
and
public inputstream getinputstream(url url) { try { inputstream str=null; isinternetpresent=icd.isconnectingtointernet(); if(isinternetpresent) { str=url.openconnection().getinputstream(); if(str!= null) { return str; }else{ new alertdialogbox(getactivity()).show(getfragmentmanager(),"mydialog"); } }else{ new alertdialogbox(getactivity()).show(getfragmentmanager(), "mydialog"); } return str; //} } catch (ioexception e) { return null; } }
below erroe getting:
this seems networkonmainthreadexception. in case resolve use asynctask run code accesses network.
it causes because of accessing network or network related tasks on main ui thread. might hang or crash app; freezes app time. why recommended use asynctask resolve issue.
how use aysnctask, read here easy:
http://developer.android.com/reference/android/os/asynctask.html
or can add line in code allow network accesses on main thread:
// allow network thread avoid networkonmainthreadexception. strictmode.threadpolicy policy = new strictmode.threadpolicy.builder() .permitall().build(); strictmode.setthreadpolicy(policy);
Comments
Post a Comment