Android and server-side data interaction (based on SOAP protocol integration Android+webservice)

Source: Internet
Author: User
Tags wsdl

Http://www.cnblogs.com/zhangdongzi/archive/2011/04/19/2020688.html

In the previous section, we used the HTTP protocol to request data using HttpClient to the server-side action. Of course, invoking a server-side method gets more data than that. WebService can also provide us with the required data,

So what is WebService? , it is a remote calling standard based on SAOP protocol, which can integrate different operating system platforms, different languages and different technologies through webservice.

We need some libraries in our PC Java client, such as XFIRE,AXIS2,CXF and so on to support access to webservice, but these libraries are not suitable for our limited resources Android phone clients, have done Java The people of me know there's ksoap. This third-party class library can help us get server-side webservice calls, of course Ksoap has provided an Android-based version of the jar package, so let's start:

First download Ksoap package: Ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar Pack

Then create a new Android project: and put the downloaded Ksoap package in the Lib directory of the Android project: right-->build path->configure build path--Select libraries,

The following is a seven-step procedure to invoke the WebService method:

First: Instantiate the Soapobject object, specify the namespace of the WebService (the namespace can be viewed from the relevant WSDL document), and invoke the method name. Such as:

View Code

The second step: If the method has parameters, set the calling method parameter

Request.addproperty ("Parameter name", "parameter value");

Step three: Set the SOAP request information (the parameter portion is the SOAP protocol version number, which is the same as the version number in the WebService you want to call):

View Code

Fourth Step: Register envelope,

(New MarshalBase64 ()). Register (envelope);

Fifth step: Build the Transport object and indicate the WSDL document URL:

View Code

Sixth step: Call WebService (where the parameter is 1: namespace + method name, 2:envelope object):

View Code

Seventh step: Parse the return data:

View Code

This will be a success. So now we're going to test it, here's an address that offers WebService weather forecasts, and here I only offer a list of the cities to get:

View Code

Then you can enter the address (WSDL) in the browser: serviceurl, you will see some methods to invoke:

We choose to obtain the main cities or provinces at home and abroad method: Getsupportprovice, and then call, you will find that the browser returned to us is an XML document:

View Code

We can use the ListView to display:

Then I'll give you all the code:

View Code

and help classes:

View Code

The above is the query of all the core code of the weather forecast, the reader can be based on comments and this article to understand the implementation of the specific, I believe that soon to understand, the results of the operation are as follows:

To this end, the next section is mainly socket communication

Android and server-side data interaction (based on SOAP protocol integration Android+webservice)

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.