Data Interaction Between Android and servers (1)

Source: Internet
Author: User

ImplementationAndroidTo interact with server data, we need some libraries in the PC machine java client, such as XFire, Axis2, and CXF, to support WebService access, however, these libraries are not suitable for android mobile clients with limited resources. Anyone who has worked on java me knows that KSOAP is a third-party class library and can help us get webService calls from the server, of course, KSOAP already provides the android-based jar package, so let's get started:

 First download the KSOAP package:

 
 
  1. ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar 

Create an android project and place the downloaded KSOAP package in the lib directory of the android project: Right-click and choose build path> configure build path -- select Libraries,

The following are seven steps to call the WebService method:

1. instantiate the SoapObject object, specify the namespace of the webService to view the namespace from the relevant WSDL document), and call the method name. For example:

 
 
  1. // Namespace
  2. Private static final String serviceNameSpace = "http://WebXml.com.cn /";
  3. // Call the method to obtain the supported cities)
  4. Private static final String getSupportCity = "getSupportCity ";
  5. // Instantiate a SoapObject object
  6. SoapObject request = new SoapObject (serviceNameSpace, getSupportCity );

2. If the method has parameters, set the call method parameters.

 
 
  1. Request. addProperty ("parameter name", "parameter value ");

3. Set the SOAP request information (the parameter part is the SOAP Protocol version number, consistent with the version number in the webService you want to call ):

 
 
  1. // Obtain the serialized Envelope
  2. SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER11 );
  3. Envelope. bodyOut = request;

4. Register Envelope,

 
 
  1. (new MarshalBase64()).register(envelope); 

5. Construct the transmission object and specify the URL of the WSDL document:

 
 
  1. // Request URL
  2. Private static final String serviceURL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx ";
  3. // Android transmission object
  4. AndroidHttpTransport transport = new AndroidHttpTransport (serviceURL );
  5. Transport. debug = true;

6. Call WebService (where the parameter is 1: namespace + method name, 2: Envelope object ):

 
 
  1. transport.call(serviceNameSpace+getWeatherbyCityName, envelope); 

7. parse the returned data:

 
 
  1. If (envelope. getResponse ()! = Null ){
  2. Return parse (envelope. bodyIn. toString ());
  3. }
  4. /**************
  5. * Parse XML
  6. * @ Param str
  7. * @ Return
  8. */
  9. Private static List <String> parse (String str ){
  10. String temp;
  11. List <String> list = new ArrayList <String> ();
  12. If (str! = Null & str. length ()> 0 ){
  13. Int start = str. indexOf ("string ");
  14. Int end = str. lastIndexOf (";");
  15. Temp = str. substring (start, end-3 );
  16. String [] test = temp. split (";");
  17. For (int I = 0; I <test. length; I ++ ){
  18. If (I = 0 ){
  19. Temp = test [I]. substring (7 );
  20. } Else {
  21. Temp = test [I]. substring (8 );
  22. }
  23. Int index = temp. indexOf (",");
  24. List. add (temp. substring (0, index ));
  25. }
  26. }
  27. Return list;
  28. }

This will be successful. Now let's test it. Here is an address that provides the webService weather forecast service. Here I only provide the city list:

 
 
  1. // Namespace
  2. Private static final String serviceNameSpace = "http://WebXml.com.cn /";
  3. // Request URL
  4. Private static final String serviceURL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx ";
  5. // Call the method to obtain the supported cities)
  6. Private static final String getSupportCity = "getSupportCity ";
  7. // Call the city method (with parameters required)
  8. Private static final String getWeatherbyCityName = "getWeatherbyCityName ";
  9. // Call the method of the province or municipality (the province or municipality that receives the support)
  10. Private static final String getSupportProvince = "getSupportProvince ";


Related Article

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.