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:
- 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:
- // Namespace
- Private static final String serviceNameSpace = "http://WebXml.com.cn /";
- // Call the method to obtain the supported cities)
- Private static final String getSupportCity = "getSupportCity ";
- // Instantiate a SoapObject object
- SoapObject request = new SoapObject (serviceNameSpace, getSupportCity );
2. If the method has parameters, set the call method parameters.
- 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 ):
- // Obtain the serialized Envelope
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope (SoapEnvelope. VER11 );
- Envelope. bodyOut = request;
4. Register Envelope,
- (new MarshalBase64()).register(envelope);
5. Construct the transmission object and specify the URL of the WSDL document:
- // Request URL
- Private static final String serviceURL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx ";
- // Android transmission object
- AndroidHttpTransport transport = new AndroidHttpTransport (serviceURL );
- Transport. debug = true;
6. Call WebService (where the parameter is 1: namespace + method name, 2: Envelope object ):
- transport.call(serviceNameSpace+getWeatherbyCityName, envelope);
7. parse the returned data:
- If (envelope. getResponse ()! = Null ){
- Return parse (envelope. bodyIn. toString ());
- }
- /**************
- * Parse XML
- * @ Param str
- * @ Return
- */
- Private static List <String> parse (String str ){
- String temp;
- List <String> list = new ArrayList <String> ();
- If (str! = Null & str. length ()> 0 ){
- Int start = str. indexOf ("string ");
- Int end = str. lastIndexOf (";");
- Temp = str. substring (start, end-3 );
- String [] test = temp. split (";");
- For (int I = 0; I <test. length; I ++ ){
- If (I = 0 ){
- Temp = test [I]. substring (7 );
- } Else {
- Temp = test [I]. substring (8 );
- }
- Int index = temp. indexOf (",");
- List. add (temp. substring (0, index ));
- }
- }
- Return list;
- }
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:
- // Namespace
- Private static final String serviceNameSpace = "http://WebXml.com.cn /";
- // Request URL
- Private static final String serviceURL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx ";
- // Call the method to obtain the supported cities)
- Private static final String getSupportCity = "getSupportCity ";
- // Call the city method (with parameters required)
- Private static final String getWeatherbyCityName = "getWeatherbyCityName ";
- // Call the method of the province or municipality (the province or municipality that receives the support)
- Private static final String getSupportProvince = "getSupportProvince ";