Async Task with multiple requests in android -
i using asynctask class update 2 different tables on sql server far code works fine i'm interested in more better , sufficient code structure of class specially in doinbackground()
okay call multiple webservices methods in single thread? can 1 suggest me?
private class update extends asynctask<void, void, integer> { private final int failed_invalid_response = 0; private final int success_get_data = 1; progressdialog progress; private string _phoneno; private string _ticket; updateticket(string phoneno,string ticket){ _phoneno=phoneno; _ticket=ticket; } @override protected void onpreexecute() { super.onpreexecute(); progress = progressdialog.show(xyz.this, "", "in progress...", false); } @override protected integer doinbackground(void... params) { method1(_phoneno); return method2(_phoneno,_ticket); } @override protected void onpostexecute(integer result) { progress.dismiss(); switch (result) { case failed_invalid_response: toast.maketext(xyz.this,"please check internet connection.",toast.length_short).show(); break; case success_get_data: toast.maketext(xyz.this, "success!", toast.length_short).show(); break; } } int method1(string phoneno,string tickets) { final string methodname = "firstmethod"; final string namespace ="http://tempuri.org/"; final string url="www.sampleurl.com"; final string soap_action="http://tempuri.org/firstmethod"; int success=0; strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); soapobject request = new soapobject(namespace, methodname); soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver11); envelope.dotnet = true; request.addproperty("phoneno", phoneno); request.addproperty("tickets", tickets); envelope.setoutputsoapobject(request); httptransportse androidhttptransport = new httptransportse(url); try{ androidhttptransport.call(soap_action,envelope); soapobject response = (soapobject) envelope.bodyin; if(response!=null){ success=1; } } catch (exception e) { e.printstacktrace(); } return success; } int method2(string phone) { final string methodname = "secondmethod"; final string namespace ="http://tempuri.org/"; final string url="www.sampleurl.com"; final string soap_action="http://tempuri.org/secondmethod"; int success=0; strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); soapobject request = new soapobject(namespace, methodname); soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver11); envelope.dotnet = true; request.addproperty("phoneno", phoneno); envelope.setoutputsoapobject(request); httptransportse androidhttptransport = new httptransportse(url); try{ androidhttptransport.call(soap_action,envelope); soapobject response = (soapobject) envelope.bodyin; if(response!=null){ success=1; } } catch (exception e) { e.printstacktrace(); } return success; } }
asynctask should used tasks/operations take quite few seconds.
asynctasks executed serially on single background thread (from api 11). long running worker can block others.
check other gotchas.
take @ heandlerthread.
Comments
Post a Comment