php - How AsyncTask implement, in this code to refresh? -


the following code extract android mysql reading json data php how can do every 10 seconds , listview refreshed, app consulted database running php reread json code generated every 10 seconds, in advance or suggestions here's code:

 public class vera extends actionbaractivity {   string myjson; private static final string tag_results="result"; private static final string tag_id = "id"; private static final string tag_name = "nombre"; private static final string tag_add ="telefono"; jsonarray peoples = null; arraylist<hashmap<string, string>> personlist; listview list;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     list = (listview) findviewbyid(r.id.listview);     personlist = new arraylist<hashmap<string, string>>();     getdata();  }    // metod show list protected void showlist(){     try {         jsonobject jsonobj = new jsonobject(myjson);         peoples = jsonobj.getjsonarray(tag_results);          for(int i=0;i<peoples.length();i++){             jsonobject c = peoples.getjsonobject(i);             string id = c.getstring(tag_id);             string name = c.getstring(tag_name);             string address = c.getstring(tag_add);              hashmap<string,string> persons = new hashmap<string,string>();              persons.put(tag_id,id);             persons.put(tag_name,name);             persons.put(tag_add,address);              personlist.add(persons);         }          listadapter adapter = new simpleadapter(                 vera.this, personlist, r.layout.list_item,                 new string[]{tag_id,tag_name,tag_add},                 new int[]{r.id.id, r.id.name, r.id.address}         );          list.setadapter(adapter);      } catch (jsonexception e) {         e.printstacktrace();     }  }      ///metod data json read php  public void getdata(){     class getdatajson extends asynctask<string, void, string>{          @override         protected string doinbackground(string... params) {             defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams());             httppost httppost = new httppost("http://tuescuelanoticias.x10.mx/selectalljson.php");              // depends on web service             httppost.setheader("content-type", "application/json");              inputstream inputstream = null;             string result = null;             try {                  httpresponse response = httpclient.execute(httppost);                 httpentity entity = response.getentity();                  inputstream = entity.getcontent();                 // json utf-8 default                 bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8);                 stringbuilder sb = new stringbuilder();                  string line = null;                 while ((line = reader.readline()) != null)                 {                     sb.append(line + "\n");                 }                 result = sb.tostring();             } catch (exception e) {                 // oops             }             {                 try{if(inputstream != null)inputstream.close();}catch(exception squish){}             }             return result;         }          @override         protected void onpostexecute(string result){             myjson=result;             showlist();         }     }     getdatajson g = new getdatajson();     g.execute(); }     } 

update post: add in oncreate metod:

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);     list = (listview) findviewbyid(r.id.listview);      myhandler.postdelayed(new runnable() {         @override         public void run() {             personlist = new arraylist<hashmap<string, string>>();             getdata();             handler.postdelayed(this, 10000);         }     }, 0);   } 


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -