The previous article is ready to work, now practice (Android Platform invoke Web service implementation number attribution query)
1. Ksoap2-android Introduction
Invoking a Web service on the Android platform relies on third-party class library KSOAP2, a SOAP WEB service client development package, primarily for resource-constrained Java environments such as applets or J2ME applications (cldc/cdc/ MIDP). We don't use KSOAP2 directly on Android, but instead use KSOAP2 Android. KSoap2 Android is an efficient, lightweight soap development package on the Android platform, equivalent to the ported version of KSoap2 on Android.
2. ksoap2-android jar Package Download
Http://code.google.com/p/ksoap2-android/
3. Invoking a procedure instance
1) Create a new project and import a third-party class library that was previously downloaded
It is recommended that you copy the class library to the project directory under the Libs file, in the import
Remember to check it (remember)
2) edit in the. java file
First step: Instantiate the Soapobject object, specify the namespace of the webservice, and invoke the method name (which was obtained in the previous article for the preparation work)
1 // namespaces and methods in a WSDL document 2 Private Static Final String targetnamespace = "http://WebXml.com.cn/"; 3 Private Static Final String function = "Getmobilecodeinfo"; 4 New Soapobject (targetnamespace, function);
The second step: set the parameter values of the calling method, if there are no parameters, you can omit
1 soapobject.addproperty ("Mobilecode", querynumber); 2 soapobject.addproperty ("UserID", null);
Note: The 1th parameter of the AddProperty method, while representing the parameter name of the calling method, is not necessarily the same as the method parameter name in the WebService class on the server side, as long as the parameters are set in the same order .
Step three: Set the SOAP request information (the parameter portion is the SOAP protocol version number, which is consistent with the version number in the WebService you want to call): This information is described by the Soapserializationenvelope object
1 New Soapserializationenvelope (SOAPENVELOPE.VER11); 2 true ; 3 envelop.setoutputsoapobject (soapobject); // or envelop.bodyout = Soapobject;
Note: Do not forget to set the Bodyout property of the Soapsoapserializationenvelope class, the value of which is the Soapobject object created in the first step.
Fourth step: Create the Httptransportsse object. You can specify the URL of the WSDL document for the WebService by using the Httptransportsse class's construction method:
New Httptransportse (WSDL);
Fifth step: Use the Call method to invoke the WebService method (where the parameter is 1: namespace + method name, 2:envelope object):
1 httpse.call (targetnamespace+function, envelop);
Sixth step: Use the GetResponse method to get the return result of the WebService method
Soapobject resultobj = (soapobject) Envelop.bodyin;
Seventh step: Parse the return data
String result = Resultobj.tostring ();
Note: Remember to join network access in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.INTERNET"/>
3) Operation effect
4) The following is the complete code:
1 PackageCom.example.webservice;2 3 Importjava.io.IOException;4 5 Importorg.apache.http.HttpServerConnection;6 ImportOrg.ksoap2.SoapEnvelope;7 ImportOrg.ksoap2.serialization.SoapObject;8 ImportOrg.ksoap2.serialization.SoapSerializationEnvelope;9 ImportOrg.ksoap2.transport.HttpTransportSE;Ten Importorg.xmlpull.v1.XmlPullParserException; One A Importandroid.app.Activity; - ImportAndroid.os.AsyncTask; - ImportAndroid.os.Bundle; the ImportAndroid.view.View; - ImportAndroid.widget.EditText; - ImportAndroid.widget.TextView; - + Public classMainactivityextendsActivity { - + PrivateEditText Edit_ponenumber; A PrivateTextView Text_result; at PrivateString Querynumber; - - //namespaces and methods in a WSDL document - Private Static FinalString targetnamespace = "http://WebXml.com.cn/"; - Private Static FinalString function = "Getmobilecodeinfo"; - Private Static FinalString WSDL = "HTTP://WEBSERVICE.WEBXML.COM.CN/WEBSERVICES/MOBILECODEWS.ASMX?WSDL"; in - to @Override + protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); the Setcontentview (r.layout.activity_main); * $Edit_ponenumber =(EditText) Findviewbyid (r.id.edit_phone_sec);Panax NotoginsengText_result =(TextView) Findviewbyid (r.id.result_text); - } the Public voidonquety (View v) + { AQuerynumber =Edit_ponenumber.gettext (). toString (); the if("". Equals (Querynumber.trim ()) | | Querynumber.length () < 7) + { -Edit_ponenumber.seterror ("Incorrect input"); $ Edit_ponenumber.requestfocus (); $Text_result.settext (""); - return; - } the //Search Mobile phone number (segment) Information - Newgetremoteinfotask (). Execute ();Wuyi } the /** - * Mobile phone number section attribution to search Wu * - * @paramphonesec Mobile phone number segment About */ $ - Public classGetremoteinfotaskextendsAsynctask<void, Void, string> - { - @Override A protectedString doinbackground (Void ... arg0) { + //TODO Auto-generated method stubs the returnGetremoteinfo (querynumber); - } $ the @Override the protected voidOnPostExecute (String result) the { the Text_result.settext (result); - Super. OnPostExecute (result); in } the the } About Publicstring Getremoteinfo (string phonesec) the { theString result =NULL; theSoapobject Soapobject =Newsoapobject (targetnamespace, function); +Soapobject.addproperty ("Mobilecode", querynumber); -Soapobject.addproperty ("UserID",NULL); theSoapserializationenvelope envelop =NewSoapserializationenvelope (SOAPENVELOPE.VER11); BayiEnvelop.dotnet =true; theEnvelop.setoutputsoapobject (Soapobject);//or envelop.bodyout = Soapobject; theHttptransportse Httpse =NewHttptransportse (WSDL); - Try { -Httpse.call (targetnamespace+function, envelop); theSoapobject resultobj =(Soapobject) Envelop.bodyin; theresult =resultobj.tostring (); the}Catch(IOException e) { the //TODO Auto-generated catch block - e.printstacktrace (); the}Catch(xmlpullparserexception e) { the //TODO Auto-generated catch block the e.printstacktrace ();94 } the returnresult; the } the}
Android Web Service Learning Summary (i)