In the previous section, we used the http protocol to request data from the server action. Of course, calling the server method to obtain data is not limited to this method. WebService can also provide us with the required data,
Then what is webService ?, It is a remote call standard based on the SAOP protocol. webservice can be used to integrate different operating system platforms, languages, and technologies.
We need some libraries in the PC machine java client, such as XFire, Axis2, and CXF, to support WebService access. However, these libraries are not suitable for android mobile clients with limited resources, anyone who has worked on java me knows that there is a third-party class library KSOAP, which can help us get webService calls from the server. Of course, KSOAP has provided a jar package based on android, let's get started:
Parse first downloads the KSOAP package: ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar package
Create an android project and place the downloaded KSOAP package in the lib directory of the android project: Right-click and choose build path> configure build path -- select Libraries,
The following are seven steps to call the WebService method:
First, instantiate the SoapObject object, specify the namespace of the webService (you can view the namespace from the relevant WSDL document), and call the method name. For example:
View Code
//Namespace
Private Static FinalString serviceNameSpace="Http://WebXml.com.cn/";
//Call method (City supported)
Private Static FinalString getSupportCity="GetSupportCity";
//Instantiate a SoapObject object
SoapObject request=NewSoapObject (serviceNameSpace, getSupportCity );
Step 2: Set the call method parameters if the method has parameters.
Request. addProperty ("parameter name", "parameter value ");
Step 3: Set the SOAP request information (the parameter part is the SOAP Protocol version number, which is consistent with the version number in the webService you want to call ):
View Code
//Obtain the serialized Envelope
SoapSerializationEnvelope envelope=NewSoapSerializationEnvelope (SoapEnvelope. VER11 );
Envelope. bodyOut=Request;
Step 4: Register Envelope,
Encode (new kerberalbase64 (). register (envelope );
Step 5: Build the transmission object and specify the URL of the WSDL document:
View Code
//Request URL
Private Static FinalString serviceURL="Http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
//Android transmission object
AndroidHttpTransport transport=NewAndroidHttpTransport (serviceURL );
Transport. debug=True;
Step 6: Call WebService (where the parameter is 1: namespace + method name, 2: Envelope object ):
View Code
transport.call(serviceNameSpace+getWeatherbyCityName, envelope);
Step 7: parse the returned data:
View Code
if(envelope.getResponse()!=null){