android - CalledFromWrongThreadException - Only the original thread that created a view hierarchy can touch its views -
this question has answer here:
i want connect mysql database via webservice.
login = (button) findviewbyid(r.id.btngiris); login.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { thread th = new thread(new runnable() { @override public void run() { runonuithread(new runnable() { @override public void run() { new authentication() .executeonexecutor(asynctask.thread_pool_executor); } }); } }); th.start();
authentication class webservice
class authentication extends asynctask<void, void, void> { final string namespace = "http://aractakipyazilimlari.com"; final string url = "http://10.0.2.2:8080/isbakapp/services/login?wsdl"; final string soap_action = "http://aractakipyazilimlari.com/authentication"; final string method_name = "authentication"; @override protected void doinbackground(void... params) { soapobject request = new soapobject(namespace, method_name); string user_name = username.gettext().tostring(); string user_password = userpassword.gettext().tostring(); propertyinfo unameprop = new propertyinfo(); unameprop.setname("plaka"); unameprop.setvalue(user_name); unameprop.settype(string.class); request.addproperty(unameprop); propertyinfo passwordprop = new propertyinfo(); passwordprop.setname("sifre"); passwordprop.setvalue(user_password); passwordprop.settype(string.class); request.addproperty(passwordprop); soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver11); envelope.setoutputsoapobject(request); httptransportse androidhttptransport = new httptransportse(url); try { androidhttptransport.call(soap_action, envelope); soapobject response = (soapobject) envelope.getresponse(); result.settext(response.tostring()); } catch (exception e) { log.e("login", e.tostring()); } return null; } }
logcat error message
android.view.viewrootimpl$calledfromwrongthreadexception: original thread created view hierarchy can touch views
1/ can't modify view in background thread, need on ui thread possible overriding onpostexecute(). method takes parameter, value returned doinbackground(), in case be: response.
also, need modify signature of asynctask:
class authentication extends asynctask<void, void, string> { }
since third type refers type of value returned doinbackground().
2/ piyush mentioned, don't need start new thread, asynctask about.
Comments
Post a Comment