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: enter image description here

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

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -