Android Development note "Call WebService"

Source: Internet
Author: User

WebService is a remote invocation standard based on the SOAP protocol, and WebService can integrate different operating system platforms, different languages and different technologies into one piece. The library that calls WebService is not available in the Android SDK, so you need to use a third-party SDK to call WebService. The PC version of the WebService client library is very rich, such as AXIS2,CXF, but these development packages are too large for Android and may not be easily ported to Android systems. Therefore, these development packages are not within our scope of consideration. WebService Client SDK for mobile phone has some, more commonly used KSOAP2, can be downloaded from http://code.google.com/p/ksoap2-android/downloads/list , copy the downloaded Ksoap2-android-assembly-2.4-jar-with-dependencies.jar package to the Lib directory of the Eclipse Project and, of course, the other directory. This jar package is also referenced in the Eclipse project.

A specific invocation of the WebService method is:

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

Soapobject Request =new Soapobject (Http://service, "getName");

The first parameter of the Soapobject class represents the namespace of webservice, and the WebService namespace can be found from the WSDL document. The second parameter represents the name of the WebService method to invoke.

(2) Set the parameter value of the calling method, if there are no parameters, you can omit the code that sets the parameter value of the method as follows:

Request.addproperty ("param1", "value");
Request.addproperty ("param2", "value");

Note that 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.

(3) Generates SOAP request information that calls the WebService method. This information is described by the Soapserializationenvelope object, and the code is:

Soapserializationenvelope envelope=new
Soapserializationenvelope (SOAPENVELOPE.VER11);
Envelope.bodyout = Request;

When you create a Soapserializationenvelope object, you need to set the version number of the SOAP protocol through the Soapserializationenvelope class's construction method. This version number needs to be set according to the version number of the server webservice. After you create the Soapserializationenvelope object, do not forget to set the Bodyout property of the Soapsoapserializationenvelope class, which is the Soapobject object created in the first step.

(4) Create a Httptransportsse object. You can specify the URL of the WSDL document for the WebService by using the Httptransportsse class's construction method:

Httptransportse ht=new Httptransportse ("http://192.168.18.17:80
/axis2/service/searchnewsservice?wsdl ");

(5) using the call method to invoke the WebService method, code:

Ht.call (Null,envelope);

The first parameter of the call method is generally null, and the 2nd parameter is the Soapserializationenvelope object created in step 3rd.

(6) using the GetResponse method to obtain the return result of the WebService method, the code:

Soapobject Soapobject = (soapobject) envelope.getresponse ();

Reference: http://www.cnblogs.com/gzggyy/archive/2011/06/21/2086140.html

Android Development note "Call 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.