web services - Convert android AsyncTask call to a separate class and call from all activities -
i new android development. have asynctask function in application. calling http request activities. in each activity using following class connect server, in activities called twice !!.
basically web developer , in such cases use single class can accessed entire application(web) , use common function same activity. difference input , out put changed.
my doubt in case can use ( convert) such function or class ? assume
- create android class ( can accessed activities )
- just make json string need specific server ( process in server )
- just pass created json created class , made http connect )
- process returned data server
- pass corresponding activity
so can use same function activities , can avoid duplicate query
can convert code such manner ?
my code
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setrequestedorientation (activityinfo.screen_orientation_portrait); setcontentview(r.layout.activity_main); login loginuser = new login(); loginuser.execute(""); } private class login extends asynctask<string, integer, string> { @override protected string doinbackground(string... surl) { try { string path = "http://www.domain_name.com/app/checksession.php"; httpclient client = new defaulthttpclient(); httpconnectionparams.setconnectiontimeout(client.getparams(), 10000); httpresponse response; jsonobject json = new jsonobject(); try { httppost post = new httppost(path); json.put("access_token", "123456"); post.setheader("json", json.tostring()); stringentity se = new stringentity(json.tostring()); se.setcontentencoding((header) new basicheader(http.content_type, "application/json")); post.setentity(se); response = client.execute(post); /* checking response */ if (response != null) { inputstream in = response.getentity().getcontent(); string = convertstreamtostring(in); jsonobject jsono = stringtojsonobj(a); string passedstringvalue = jsono.getstring("result"); if(passedstringvalue.equals("1")){ flags=1; //log.v("tagg", "success"); } else { flags=0; //log.v("tagg", "failed !"); } } } catch (exception e) { e.printstacktrace(); } } catch (exception e) { } return null; } @override protected void onpreexecute() { super.onpreexecute(); showdialogue("login processing", "loading"); } @override protected void onprogressupdate(integer... progress) { super.onprogressupdate(progress); } @override protected void onpostexecute(string result) { if(flags.equals(1)){ itent homepage = new intent(mainactivity.this, registerdevice.class); startactivity(homepage); finish(); } else { intent homepage = new intent(mainactivity.this, loginactivity.class); startactivity(homepage); finish(); } super.onpostexecute(result); } } }
please 1 help/advise in advance
extract class different file , make public
public class login extends asynctask<object, integer, string> { private iloginlistener listener; @override protected string doinbackground(object... arg0) { try { this.listener = (iloginlistener) arg0[0]; //you can send url in obj array string theurl = (string) arg0[1]; string path = "http://www.domain_name.com/app/checksession.php"; httpclient client = new defaulthttpclient(); httpconnectionparams.setconnectiontimeout(client.getparams(), 10000); httpresponse response; jsonobject json = new jsonobject(); try { httppost post = new httppost(path); json.put("access_token", "123456"); post.setheader("json", json.tostring()); stringentity se = new stringentity(json.tostring()); se.setcontentencoding((header) new basicheader(http.content_type, "application/json")); post.setentity(se); response = client.execute(post); /* checking response */ if (response != null) { inputstream in = response.getentity().getcontent(); string = convertstreamtostring(in); jsonobject jsono = stringtojsonobj(a); string passedstringvalue = jsono.getstring("result"); if(passedstringvalue.equals("1")){ flags=1; //log.v("tagg", "success"); } else { flags=0; //log.v("tagg", "failed !"); } } } catch (exception e) { e.printstacktrace(); } } catch (exception e) { } return null; } @override protected void onpreexecute() { super.onpreexecute(); showdialogue("login processing", "loading"); } @override protected void onprogressupdate(integer... progress) { super.onprogressupdate(progress); } @override protected void onpostexecute(string result) { listener.loginsessionchecklistener(flag.equals(1)); super.onpostexecute(result); } }
regarding other question, have interface that, this:
public interface iloginlistener { public void loginsessionchecklistener(someneeded value); }
i implement interface in class need postexecute result , in overriden method can want result of task. class user this:
public class someclass implements iloginlistener { //call class: login logintask = new login(); object[] someparams = new object[2]; //add listener someparams[0] = someclass.this //add url someparams[1] = someurlstring; logintask.execute(someparams); @override public void loginsessionchecklistener(someneeded value){ //do stuff results } }
Comments
Post a Comment