android interfacing with mysql database -
i'm trying fetch username , password values mysql database using soap. i'm getting 'android.os.networkonmainthreadexception', please tell me how use asynctask avoid exception.
package com.example.androidloginexampleactivity;
import java.io.ioexception; import org.ksoap2.soapenvelope; import org.ksoap2.serialization.propertyinfo; import org.ksoap2.serialization.soapobject; import org.ksoap2.serialization.soapprimitive; import org.ksoap2.serialization.soapserializationenvelope; import org.ksoap2.transport.httptransportse; import android.app.activity; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview; import android.widget.toast; public class androidloginexampleactivity extends activity { private final string namespace = "http://10.0.2.2:80/urn:stockserver"; private final string url = "http://10.0.2.2:80/stockserver1.php?wsdl"; //private final string url = " http://10.0.2.2:8080/webapplication2/login?tester"; private final string soap_action = "http://10.0.2.2:80/urn:stockserver#getstockquote"; private final string method_name = "getstockquote"; /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_android_login_example); button login = (button) findviewbyid(r.id.btn_login); login.setonclicklistener(new view.onclicklistener() { public void onclick(view arg0) { loginaction(); } }); } private void loginaction(){ soapobject request = new soapobject(namespace, method_name); edittext username = (edittext) findviewbyid(r.id.tf_username); string user_name = username.gettext().tostring(); edittext userpassword = (edittext) findviewbyid(r.id.tf_password); string user_password = userpassword.gettext().tostring(); //pass value username variable of web service propertyinfo unameprop =new propertyinfo(); unameprop.setname("username");//define variable name in web service method unameprop.setvalue(user_name);//set value username variable unameprop.settype(string.class);//define type of variable request.addproperty(unameprop);//pass properties variable //pass value password variable of web service propertyinfo passwordprop =new propertyinfo(); passwordprop.setname("password"); passwordprop.setvalue(user_password); passwordprop.settype(string.class); request.addproperty(passwordprop); soapserializationenvelope envelope = new soapserializationenvelope(soapenvelope.ver10); envelope.setoutputsoapobject(request); httptransportse androidhttptransport = new httptransportse(url); try{ androidhttptransport.call(soap_action, envelope); soapprimitive response = (soapprimitive)envelope.getresponse(); textview result1 = (textview) findviewbyid(r.id.tv_status); result1.settext(response.tostring()); toast.maketext(getapplicationcontext(), response.tostring(), 5).show(); } catch(exception e){ toast.maketext(getapplicationcontext(), e.tostring(), 5).show(); } } }
convert loginaction inner class extends asynctask. override preexecute method of setup. use onexecute method network communication. processing results in postexecute method.
here tutorial: http://codeoncloud.blogspot.com/2013/07/android-web-service-access-using-async.html
Comments
Post a Comment