Method example of calling WebService for Android Development

This article describes the method of calling WebService by Android development. Share with you for your reference, as follows:

Web service is a remote calling standard based on SOAP Protocol. Different operating system platforms, different languages and different technologies can be integrated into one through web service. There is no library to call WebService in the Android SDK, so you need to use a third-party SDK to call WebService. The PC version of WebService client libraries are very rich, such as Axis2, CXF, etc., but these development packages are too large for the Android system and may not be easy to transplant to the Android system. Therefore, these development packages are not in our consideration. There are some SDKs suitable for mobile phone WebService clients. The more commonly used one is ksoap2, which can be downloaded from http://code.google.com/p/ksoap2-android/downloads/list Download; Copy the downloaded ksoap2-android-assembly-2.4-jar-with-dependencies.jar package to the Lib directory of the eclipse project. Of course, it can also be placed in other directories. At the same time, reference the jar package in the eclipse project.

The specific methods of calling WebService are:

(1) Specify the namespace of WebService and the method name to call, such as:

The first parameter of the soapobject class represents the namespace of the WebService, which can be found in the WSDL document. The second parameter represents the name of the WebService method to call.

(2) Set the parameter value of the calling method. If there is no parameter, it can be omitted. The code for setting the parameter value of the method is as follows:

It should be noted that although the first parameter of addproperty method represents the parameter name of the calling method, the parameter value is not necessarily consistent with the method parameter name in the WebService class of the server, as long as the order of setting parameters is consistent.

(3) Generate soap request information that calls the WebService method. This information is described by the soapserializationenvelope object. The code is:

When creating a soapserializationenvelope object, you need to set the version number of the soap protocol through the constructor of the soapserializationenvelope class. The version number needs to be set according to the version number of the server WebService. After creating the soapserializationenvelope object, don't forget to set the bodyout property of the soapserializationenvelope class. The value of this property is the soapobject object created in the first step.

(4) Create an httptransportsse object. The URL of the WSDL document of the WebService can be specified through the construction method of the httptransportsse class:

(5) Use the call method to call the WebService method. Code:

The first parameter of the call method is generally null, and the second parameter is the soapserializationenvelope object created in step 3.

(6) Use the getresponse method to obtain the return result of the WebService method. Code:

The following is a simple example of realizing the weather viewing function:

More readers interested in Android related content can view the topics of this site: summary of Android basic component usage, summary of Android view skills, summary of Android resource operation skills, summary of Android operation JSON format data skills, introduction and advanced tutorial of Android development, summary of activity operation skills of Android programming, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>