Android and server-side data interaction (Integrate android + webservice Based on SOAP protocol)

Source: Internet
Author: User

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){

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.