12.21-Android WebService (based on KSOAP2), ksoap2webservice

Source: Internet
Author: User

12.21-Android WebService (based on KSOAP2), ksoap2webservice

Ksoap2_connect to WebService

* ***** I am a small knowledge point ********

Ksoap2 provides better support for soap serialization. Ksoap2 is a lightweight implementation framework of the j2's platform. It provides the soap protocol message

Assembly, network post, network return, resolution, and other functions.

**********

1. Import the KSOAP2.jar package to the Project

2. Access the. net URL and find the agreed URL namespace and access method.

String SERVICE_NS = "http://tempuri.org /";
String methodName = "mydata ";

3. Create a SoapObject and input the namespace of the Web Service to be called. The Web Service method name

SoapObject soapObject = new SoapObject (SERVICE_NS, methodName );

4. Pass the parameters to the Web Service server and call the addProperty (String name, Object value) method of the SoapObject Object to set the parameters. (Name is the parameter name agreed by android and. net)

SoapObject. addProperty ("L", con );

5. Use the SOA1.1 protocol to create an Envelop object and set compatibility with the WebService provided by. net.

// Generate soap information for WebService method call and specify the Soap version

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER10 );
Envelope. bodyOut = soapObject;

Envelope. dotNet = true; // whether to call the WebService developed by DotNet

6. Create an HttpTransportSE transmission object and pass in the WebService server address

String SERVICE_URL = "http: // 192.168.66.66: 82/data. asmx ";

HttpTransportSE ht = new HttpTransportSE (SERVICE_URL );

7. call the call method of the HttpTransportSE object. The first parameter of the call is the namespace + Web service method name, and the second parameter is the object of SoapSerializationEnvelope.

Ht. call (SERVICE_NS + methodName, envelope );

8. If the returned value is a string, the method for receiving the returned parameters is: envelope and envelioe. getResponse, the object of SoapSerializationEnvelope, and receiving the returned value.

If (envelope. getResponse ()! = Null ){

String s = envelope. getResponse (). toString ();

}

***********

1. The namespace must be consistent with the URL format

2. The method name must be consistent with. net.

3. Note that the SOAP version is 10/11/12. You can check the first version of the xml file in. net or try all three.

4. During the call, it cannot be placed in the main thread, but it must be placed in the asynchronous thread call.

*************

Package test. example. ksoap2; import java. io. IOException; import java. util. arrayList; import java. util. list; import org. ksoap2.SoapEnvelope; import org. ksoap2.serialization. soapObject; import org. ksoap2.serialization. soapSerializationEnvelope; import org. ksoap2.transport. httpTransportSE; import org. xmlpull. v1.XmlPullParserException; public class ConnectWebService {/*** determines whether the returned value is correct ** @ param con * @ return * /Public static boolean getData (String con) {String SERVICE_URL = "http: // 192.168.66.66: 82/data. asmx "; String SERVICE_NS =" http://tempuri.org/"; String methodName =" mydata "; // create a SoapObject object, // when creating this object, you need to input the namespace and Web Service Method Name of the Wb Service to be called; SoapObject soapObject = new SoapObject (SERVICE_NS, methodName ); // If a parameter is required to be sent to the Web Service server, // call the addProperty (String name, Object value) method of the SoapObject Object to set the parameter, // The name parameter of the method specifies the parameter name; // The value parameter specifies the parameter value soapObject. addProperty ("L", con); // use the SOA1.1 protocol to create the Envelop object SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER10); envelope. bodyOut = soapObject; // whether to call the WebService developed by DotNet. the Web Service provided by. net maintains good compatibility with envelope. dotNet = true; try {// transfer an object through HttpTransportSE, and pass in the WebService server address HttpTransportSE ht = new HttpTransportSE (SERVICE_URL ); // Call the call () method of the HttpTransportSE object. The first parameter of call is soapAction, and the second parameter is SoapSerializationEvelope. call (SERVICE_NS + methodName, envelope); if (envelope. getResponse ()! = Null) {// get the SOAP message returned by the server response/** SoapObject result = (SoapObject) envelope. bodyIn; SoapObject * detail = (SoapObject) * result. getProperty (methodName + "Result"); * // ** get the returned data SoapObject object = (SoapObject) envelope. bodyIn; * Get the returned result String result = object. getProperty (0 ). toString (); * // parse the SOAP message of the server-String s = envelope. getResponse (). toString (); if (s. equals ("OK") {return true ;}} catch (IOException e) {e. printStackTrace ();} catch (XmlPullParserException e) {System. out. println ("********************* exception");} return false ;}}

* *************** I am a daily cool Tao ***************** **

 

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.