Using AsyncTask with android -
my code ok, no problem.i did not use onpostexecute() method. code run , layout show fine. want use onpostexecute method showing ui. how can use onpostexecute in code? because code looping , adding value in listview. can not understand how can use onpostexecute in code. please me !
private class asynccaller extends asynctask<void, void, void> { progressdialog pdloading = new progressdialog(mainactivity.this); @override protected void onpreexecute() { super.onpreexecute(); //this method running on ui thread pdloading.setmessage("\tloading..."); pdloading.show(); } @override protected void doinbackground(void... params) { //this method running on background thread don't update ui frome here //do long running http tasks here,you dont want pass argument , u can access parent class' variable url on here try { parsequery<parseobject> myobject = parsequery.getquery("allpost"); myobject.findinbackground(new findcallback<parseobject>() { @override public void done(list<parseobject> objects, parseexception e) { (parseobject parseobject : objects) { // toast.maketext(getapplicationcontext(), // parseobject.getstring("post"), toast.length_long).show(); list.add(parseobject.getstring("post")); adapter = new arrayadapter<string>(getapplicationcontext(), r.layout.list_view, list); setlistadapter(adapter); } } }); thread.sleep(300); } catch (interruptedexception e1) { // todo auto-generated catch block e1.printstacktrace(); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); //this method running on ui thread pdloading.dismiss(); } }
private class webservicetask extends asynctask<string, integer, string> { public static final int post_task = 1; private static final string tag = "webservicetask"; // connection timeout, in milliseconds (waiting connect) private static final int conn_timeout = 3000; // socket timeout, in milliseconds (waiting data) private static final int socket_timeout = 5000; private int tasktype = post_task; private context mcontext = null; private arraylist<namevaluepair> params = new arraylist<namevaluepair>(); public webservicetask(int tasktype, context mcontext, string processmessage) { this.tasktype = tasktype; this.mcontext = mcontext; } public void addnamevaluepair(string name, string value) { params.add(new basicnamevaluepair(name, value)); } protected string doinbackground(string... urls) { string url = urls[0]; string result = ""; httpresponse response = doresponse(url); if (response == null) { return result; } else { try { result = inputstreamtostring(response.getentity().getcontent()); } catch (illegalstateexception e) { log.e(tag, e.getlocalizedmessage(), e); } catch (ioexception e) { log.e(tag, e.getlocalizedmessage(), e); } } return result; } @override protected void onpostexecute(string response) { handleresponse(response); } // establish connection , socket (data retrieval) timeouts private httpparams gethttpparams() { httpparams htpp = new basichttpparams(); httpconnectionparams.setconnectiontimeout(htpp, conn_timeout); httpconnectionparams.setsotimeout(htpp, socket_timeout); return htpp; } private httpresponse doresponse(string url) { // use our connection , data timeouts parameters our // defaulthttpclient httpclient httpclient = new defaulthttpclient(gethttpparams()); httpresponse response = null; try { switch (tasktype) { case post_task: httppost httppost = new httppost(url); // add parameters httppost.setentity(new urlencodedformentity(params)); response = httpclient.execute(httppost); break; } } catch (exception e) { display("remote database can not connected.\nplease check network connection."); log.e(tag, e.getlocalizedmessage(), e); return null; } return response; } private string inputstreamtostring(inputstream is) { string line = ""; stringbuilder total = new stringbuilder(); // wrap bufferedreader around inputstream bufferedreader rd = new bufferedreader(new inputstreamreader(is)); try { // read response until end while ((line = rd.readline()) != null) { total.append(line); } } catch (ioexception e) { log.e(tag, e.getlocalizedmessage(), e); } // return full string return total.tostring(); } } public void handleresponse(string response) { //display("json responce : "+response); try { jsonobject jso = new jsonobject(response); string uname = "hi"; if( uname.equalsignorecase("") || uname==null) { display("project report(s) can not retrieved."); } else { // int count=integer.parseint(uname); // display("number of projects have been handling in afl right now: "+count); list1=new arraylist<string>(); list12=new arraylist<string>(); jsonarray array=jso.getjsonarray("jobs"); jsonarray array1=jso.getjsonarray("jobs1"); for(int i=0;i<array.length();i++) { list1.add(array.getjsonobject(i).getstring("branchname3")); list12.add(array1.getjsonobject(i).getstring("branchname4")); } itr=list1.iterator(); itr1=list12.iterator(); while(itr.hasnext()&&itr1.hasnext()) { //str1=itr.next()+"\n"; todoitems.add(0, itr.next().tostring()); cad.add(0, itr1.next().tostring()); aa.notifydatasetchanged(); a1.notifydatasetchanged(); } //tv1.settext(str1); } } catch (exception e) { log.e(tag, e.getlocalizedmessage(), e); return; } }
use example,here did onpost execute
Comments
Post a Comment