Use KSOAP2 in Android to invoke Webservice__android

Source: Internet
Author: User
Tags soap serialization wsdl

The

WebService is a remote invocation standard based on the SOAP protocol. Through the webservice can be different operating system platform, different languages, different technologies integrated together. There is no library to invoke WebService in the Android SDK, so you need to use a Third-party class library (KSOAP2) to invoke WebService. In this article, we will describe the specifics of calling WebService in Android, and at the end give a complete example to show how to use KSOAP2 to invoke WebService. Installing a Third-party class Library: KSOAP2         pc version of the WebService client class library is very rich, for example, Axis2, CXF and so on, but these libraries are too big for Android and may not be easy to migrate to Android systems. As a result, these development packages are not within our scope of consideration. The WebService client class library for mobile phones also has some. This example uses a more commonly used KSOAP2. Readers can download the Android version of KSOAP2 from the address below.  http://code.google.com/p/ksoap2-android/downloads/list?can=1&q=&colspec=filename+summary+ Uploaded+size+downloadcount
         copies the downloaded jar file to the Lib directory of the Eclipse project (if the directory is not , you can create a new one and, of course, you can put it in a different directory. The jar package is referenced in Eclipse Engineering, and the referenced Eclipse Engineering directory structure is shown in Figure 1. Figure 1 Referencing the KSOAP2 development package   using KSOAP2 to invoke WebService        The reader can invoke the WebService method in the following 6 steps. 1.  specifies the namespace of the WebService and the method name of the call, as follows:

Soapobject Request =new Soapobject ("Http://service", "getName");
The 1th parameter of the Soapobject class represents the namespace of the webservice, and the WebService namespace can be found from the WSDL document.   The 2nd parameter represents the name of the WebService method to invoke. 2. Sets the parameter value of the calling method, which is optional and can be omitted if the method has no parameters. The code for setting the parameter value of the method is as follows:
Request.addproperty ("param1", "value1");
Request.addproperty ("param2", "value2");
Note that the 1th parameter of the AddProperty method, while representing the parameter name of the calling method, does not necessarily match the method parameter name in the WebService class on the server side, as long as the order of the parameters is set. 3. Generate SOAP request information that invokes the WebService method. This information is described by the Soapserializationenvelope object, and the code is as follows:
Soapserializationenvelope envelope =new soapserializationenvelope (SOAPENVELOPE.VER11);
Envelope.bodyout = Request;
When creating a Soapserializationenvelope object, you need to set the version number of the SOAP protocol through the Soapserializationenvelope class's construction method. The version number needs to be set according to the WebService version number of the service side.   After you create the Soapserializationenvelope object, do not forget to set the Bodyout property of the Soapserializationenvelope class, which is the Soapobject object created in step 1th. 4. Create the Httptransportse object. The Httptransportse class constructor allows you to specify the URL of a WebService WSDL document, as follows:
Httptransportse HT =
Newhttptransportse ("http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl");
5. Invoke the WebService method using the call method with the following code:
Ht.call (null, envelope);
The 1th parameter of the call method is generally null, and the 2nd parameter is the Soapserializationenvelope object created in step 3rd. 6. Use the GetResponse method to obtain the return result of the WebService method, the code is as follows:
Soapobject Soapobject = (soapobject) envelope.getresponse ();
Example: Querying product information through WebService         This example involves a WebService server-side program and a ophone client program. The reader can copy the server-side program (AXIS2 directory) directly to the <tomcat installation directory >\webapps directory, then start Tomcat and enter the following url:http://localhost:8080/axis2 in the browser address bar           If the page shown in Figure 2 is displayed in the browser, the server-side program has been successfully installed. Figure 2 webservice Homepage           This service-side WebService program is Searchproductservice, In fact, Searchproductservice is a Java class, just using AXIS2 to map it to webservice. There is a getproduct method in this class. This method has a string type parameter that represents the product name. The method returns a Product object that has 3 properties: Name, Price, and ProductNumber. Readers can use the URL below to view the Searchproductservice WSDL document. HTTP://LOCALHOST:8080/AXIS2/SERVICES/SEARCHPRODUCTSERVICE?WSDL          The page that displays the WSDL document is shown in Figure 3. Figure 3&NBSP;WSDL Document            in the black box in Figure 3 is the WebService namespace, is also the 1th parameter value of the construction method of the Soapobject class. This WebService program can be tested directly using the following URL. Http://localhost:8080/axis2/services/SearchProductService/getProduct?param0=iphone            Testing ofThe result is shown in Figure 4. Figure 4  Test GetProduct method           from the test results shown in Figure 4, we can see that AXIS2 converts the Product object returned by the GetProduct method directly to the XML document, which is actually a SOAP format, to return.          Below we will write the OPhone client program that invokes webservice based on the steps described earlier using KSOAP2, as follows:
01 Packagenet.blogjava.mobile.wsclient;
02
03 Importorg.ksoap2.SoapEnvelope;
04 Importorg.ksoap2.serialization.SoapObject;
05 Importorg.ksoap2.serialization.SoapSerializationEnvelope;
06 Importorg.ksoap2.transport.HttpTransportSE;
07 importandroid.app.Activity;
08 Importandroid.os.Bundle;
09 Importandroid.view.View;
10 Importandroid.view.View.OnClickListener;
11 Importandroid.widget.Button;
12 Importandroid.widget.EditText;
13 Importandroid.widget.TextView;
14
15 Publicclass Main Extendsactivity implements Onclicklistener
16 {
17 @Override
18 Publicvoid OnClick (view view)
19 {
20 EditText etproductname = (edittext) Findviewbyid (r.id.etproductname);
21st TextView Tvresult = (TextView) Findviewbyid (R.id.tvresult);
22 The url,192.168.17.156 of the WSDL document is the ID address of the PC
23 String serviceurl = "http://192.168.17.156:8080/axis2/services/SearchProductService?wsdl";
24 Defines the WebService method name of the call
25 String methodname = "GetProduct";
26 Step 1th: Create the Soapobject object and specify the WebService namespace and the method name of the call
27 Soapobject Request =new Soapobject ("Http://service", MethodName);
28 Step 2nd: Set the parameters of the WebService method
29 Request.addproperty ("ProductName", Etproductname.gettext (). toString ());
30 Step 3rd: Create the Soapserializationenvelope object and specify the version of WebService
31 Soapserializationenvelope envelope =new soapserializationenvelope (SOAPENVELOPE.VER11);
32 Setting the Bodyout Property
33 Envelope.bodyout = Request;
34 Step 4th: Create the Httptransportse object and specify the URL of the WSDL document
35 Httptransportse HT =new Httptransportse (serviceurl);
36 Try

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.