java - AsyncTask with HTTP get and fetch of .xml -


i developing android app clicking on buttons control digital state of arduino board. arduino responses xml file created during runtime and, fetching contents, i'd display state of pins.

now, realize in fact app works can't handle fetchxml() method in asynctask instance. ui freezes , don't want that.

this code of main activity:

public class mainactivity extends actionbaractivity {  private string url = "http://192.168.0.254/?domotica"; private edittext button1,button2,button3,button4; private handlexml obj;  @override protected void oncreate(bundle savedinstancestate) {    /*  if (android.os.build.version.sdk_int > 9) {         strictmode.threadpolicy policy = new     strictmode.threadpolicy.builder().permitall().build();         strictmode.setthreadpolicy(policy);     }   */     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     button1 = (edittext)findviewbyid(r.id.edittext1);     button2 = (edittext)findviewbyid(r.id.edittext2);     button3 = (edittext)findviewbyid(r.id.edittext3);     button4 = (edittext)findviewbyid(r.id.edittext4);        button led1on = (button) findviewbyid(r.id.led_1on);     button led1off = (button) findviewbyid(r.id.led_1off);      led1on.setonclicklistener(new view.onclicklistener() {         public void onclick(view v){             //commandarduino("http://192.168.0.254/?b1o");             invio inv = new invio();             inv.execute("http://192.168.0.254/?b1o");          }     });      led1off.setonclicklistener(new view.onclicklistener() {         public void onclick(view v){             //commandarduino("http://192.168.0.254/?b1f");             invio inv = new invio();             inv.execute("http://192.168.0.254/?b1f");         }     });  }  private class invio extends asynctask<string,void,string> {   @override  protected string doinbackground(string... params) {      int count = params.length;       (int = 0; < count; i++) {          try {              httpclient httpclient = new defaulthttpclient();              httpclient.execute(new httpget(params[i]));          } catch (exception e) {              e.printstacktrace();           }      }      return null;  } }  public void open(view view){     string finalurl = url;     obj = new handlexml(finalurl);     obj.fetchxml();     while(obj.parsingcomplete);     button1.settext(obj.getbutton1());     button2.settext(obj.getbutton2());     button3.settext(obj.getbutton3());     button4.settext(obj.getbutton4());  }  /*public void commandarduino(string urlz) {   }*/ @override public boolean oncreateoptionsmenu(menu menu) {     // inflate menu; adds items action bar if present.     getmenuinflater().inflate(r.menu.menu_main, menu);     return true; }  @override public boolean onoptionsitemselected(menuitem item) {     // handle action bar item clicks here. action bar     // automatically handle clicks on home/up button, long     // specify parent activity in androidmanifest.xml.     int id = item.getitemid();      //noinspection simplifiableifstatement     if (id == r.id.action_settings) {         return true;     }      return super.onoptionsitemselected(item); } } 

and that's code of xml class used handle , fetch xml file , contents:

public class handlexml {  private string button1 = "button1"; private string button2 = "button2"; private string button3 = "button3"; private string button4 = "button4"; private string urlstring = null; private xmlpullparserfactory xmlfactoryobject;  public volatile boolean parsingcomplete = true;  public handlexml(string url){     this.urlstring = url; } public string getbutton1(){     return button1; } public string getbutton2(){     return button2; } public string getbutton3(){     return button3; } public string getbutton4(){     return button4; }  public void parsexmlandstoreit(xmlpullparser myparser) {     int event;     string text=null;     try {         event = myparser.geteventtype();         while (event != xmlpullparser.end_document) {             string name=myparser.getname();             switch (event){                 case xmlpullparser.start_tag:                     break;                 case xmlpullparser.text:                     text = myparser.gettext();                     break;                  case xmlpullparser.end_tag:                     if(name.equals("button1")){                         button1 = text;                     }                     else if(name.equals("button2")){                         button2 = text;// myparser.gettext();                     }                     else if(name.equals("button3")){                         button3 = text;//myparser.gettext();                     }                     else if(name.equals("button4")){                         button4 = text;//myparser.gettext();                     }                     else{                     }                     break;             }             event = myparser.next();          }         parsingcomplete = false;     } catch (exception e) {         e.printstacktrace();     }  } public void fetchxml(){     thread thread = new thread(new runnable(){         @override         public void run() {             try {                 url url = new url(urlstring);                 httpurlconnection conn = (httpurlconnection)                         url.openconnection();                 conn.setreadtimeout(1000 /* milliseconds */);                 conn.setconnecttimeout(1500 /* milliseconds */);                 conn.setrequestmethod("get");                 conn.setdoinput(true);                 conn.connect();                 inputstream stream = conn.getinputstream();                  xmlfactoryobject = xmlpullparserfactory.newinstance();                 xmlpullparser myparser = xmlfactoryobject.newpullparser();                  myparser.setfeature(xmlpullparser.feature_process_namespaces                         , false);                 myparser.setinput(stream, null);                 parsexmlandstoreit(myparser);                 stream.close();             } catch (exception e) {                 e.printstacktrace();             }         }     });      thread.start(); } } 

as suggested @njzk problem in statment:

while(obj.parsingcomplete); 

that creates endless loop, i've removed , working.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -