The purpose of this blog is for me to use later webservice, for reference, here to record
This blog is written, using WebService query mobile phone number, reference article is Android platform call WebService detailed, but this article is 2011, now the network operation must be in the sub-thread.
Is:
The comments are very detailed, for everyone has a great help ~ ~ ~
Package Lgx.acc.updatedemo;import Java.io.ioexception;import Org.ksoap2.soapenvelope;import org.ksoap2.SoapFault; Import Org.ksoap2.serialization.soapobject;import Org.ksoap2.serialization.soapserializationenvelope;import Org.ksoap2.transport.httptransportse;import Org.xmlpull.v1.xmlpullparserexception;import android.app.Activity; Import Android.os.bundle;import android.os.handler;import Android.os.message;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;public class Mainactivity extends Activity {private EditText phonesecedittext;private TextView Resultview;private Button Querybutton; Handler Handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//display it in Handler resultview.settext ( Msg.obj.toString ()); Super.handlemessage (msg);}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main);p Honesecedittext = (EditText) Findviewbyid (r.id.phone_sec); Resultview = (TextView) Findviewbyid (R.id.result_text); Querybutton = (Button) Findviewbyid (R.ID.QUERY_BTN); Querybutton.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {//Mobile number (segment) String phonesec = Phonesecedittext.gettext (). toString (). Trim ();/ Simple to determine whether the user entered the mobile phone number (segment) is legal if ("". Equals (phonesec) | | Phonesec.length () < 7) {//Give error Phonesecedittext.seterror ("You entered the mobile number (segment) is wrong! ");p Honesecedittext.requestfocus ();//The TextView that will display the results of the query is emptied Resultview.settext (" "); Query mobile number (segment) Information getremoteinfo (PHONESEC);});} private void Getremoteinfo (String phonesec) {final String namespace = "http://WebXml.com.cn/"; String methodName = "Getmobilecodeinfo"; String serviceurl = "Http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; final string soapaction = namespace + methodname;//1. Create a Soapobject namespace and call the method name Soapobject Soapobject = new Soapobject (namespace, methodName);//2. Configure the two parameters of the Soapobject soapobject.addproperty ("MobilecOde ", Phonesec), Soapobject.addproperty (" UserId "," ");//3. Instantiate Soapserializationenvelope, the version number of the SOAP protocol passed in webservice final soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER10);//4. Set whether to invoke Yes. NET development of Webserviceenvelope.setoutputsoapobject (soapobject); envelope.dotnet = true;//5. Creating Httptransportse objects, Delivery WebService server address final Httptransportse Transportse = new Httptransportse (serviceurl); Runnable Runnable = new Runnable () {@Overridepublic void run () {try {//Call Webservicetransportse.call (SOAPAction, envelope );} catch (IOException e) {e.printstacktrace ();} catch (Xmlpullparserexception e) {e.printstacktrace ();} try {//Get data Object result = Envelope.getresponse (); Message msg = Message.obtain (); msg.obj = Result;handler.sendmessage (msg);} catch (SoapFault e) {//TODO auto-generated catch Blocke.printstacktrace ();}}; New Thread (runnable). Start ();}}
For other content on this blog, you can view the Android platform invoke WebService in detail and speak very detailed
For more basic knowledge about WebService, you can view WebService introduction to Android development
Complete explanation ~ ~ ~
How to WebService under the Android platform