使用ksoap2 for android 调用WebService

1,建立WebService服务器
    这里使用Apache的axis2来建立WebService,并使用RPC的方式。
    (1) 首先下载Axis,然后将解压后的webapp下面的东东拷贝到Tomcat的webapp下面,目录为/webapp/axis。去掉一些不相干的部署文本,如果classes下面的东东。
   写server-config.wsdd文件,放置到axis的WEB-INF目录下面。具体如下

 1 <? xml version="1.0" encoding="UTF-8" ?>
 2 < deployment  xmlns ="http://xml.apache.org/axis/wsdd/"
 3     xmlns:java ="http://xml.apache.org/axis/wsdd/providers/java" >
 4
 5      < globalConfiguration >
 6          < parameter  name ="sendMultiRefs"  value ="true"   />
 7          < parameter  name ="disablePrettyXML"  value ="true"   />
 8          < parameter  name ="dotNetSoapEncFix"  value ="true"   />
 9          < parameter  name ="enableNamespacePrefixOptimization"  value ="false"   />
10          < parameter  name ="sendXMLDeclaration"  value ="true"   />
11          < parameter  name ="sendXsiTypes"  value ="true"   />
12          < parameter  name ="attachments.implementation"
13             value ="org.apache.axis.attachments.AttachmentsImpl"   />
14      </ globalConfiguration >
15
16      < handler  type ="java:org.apache.axis.handlers.http.URLMapper"
17         name ="URLMapper"   />
18      < service  name ="HelloService"  provider ="java:RPC" >
19          < parameter  name ="className"  value ="cn.vaga.testws.HelloService"   />
20          < parameter  name ="scope"  value ="request"   />
21          < parameter  name ="allowedMethods"  value ="*"   />
22         
23          < operation  name ="sayHello"  qname ="operNS:sayHello"  xmlns:operNS ="http://testws.vaga.cn"  returnQName ="sayHelloResult"  returnType ="rtns:ListOfHello"  xmlns:rtns ="http://testws.vaga.cn"   />
24  
25      < typeMapping
26         xmlns:ns ="http://localhost:8080/TestWS/services/HelloService"
27        qname ="ns:ListOfHello"
28        type ="java:java.lang.String[]"
29        serializer ="org.apache.axis.encoding.ser.ArraySerializerFactory"
30        deserializer ="org.apache.axis.encoding.ser.ArrayDeserializerFactory"
31        encodingStyle ="http://schemas.xmlsoap.org/soap/encoding/"
32       />     
33          < messageReceiver
34              class ="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"   />
35             
36
37      </ service >
38      < transport  name ="http" >
39          < requestFlow >
40              < handler  type ="URLMapper"   />
41          </ requestFlow >
42      </ transport >
43 </ deployment >

   (2) 写业务类HelloService.java

  
 1 package  cn.vaga.testws;
 2
 3
 4 public   class  HelloService {
 5      public  String[] sayHello() {
 6          return   new  String[] { " www " , " 1232 " , " 12dd " } ;
 7     }

 8     
 9      public  String sayHelloToPerson(String name) {
10           if (name  ==   null   ||   "" .equals(name)) {
11               name  =   " nobody " ;
12          }

13          
14           return   " Hello  "   +  name;
15     }

16 }

    然后将编译好的class文件放置到axis/WEB-INF/classes下面,注意包结构。启动Tomcat。
   (3) 在浏览器中输入http://192.168.1.110:8080/TestWS/services/HelloService?wsdl ,OK,如果出现了XML代码表示你成功了。

2、使用ksoap2的客户端
     (1) 建立客户端类

 1 package  cn.vaga.testwsclient;
 2
 3 import  java.util.List;
 4 import  java.util.Vector;
 5
 6 import  org.ksoap2.SoapEnvelope;
 7 import  org.ksoap2.serialization.SoapObject;
 8 import  org.ksoap2.serialization.SoapSerializationEnvelope;
 9 import  org.ksoap2.transport.AndroidHttpTransport;
10
11 public   class  Client   {
12      private   static   final  String NAMESPACE  =   " http://testws.vaga.cn " ;    
13      private   static  String URL  =   " http://192.168.1.110:8080/TestWS/services/HelloService " ;
14      private   static   final  String METHOD_NAME  =   " sayHello " ;
15      private   static  String SOAP_ACTION  =  NAMESPACE  +   " / "   +  METHOD_NAME; 
16      public   static  String getHello()  {
17          try   {
18             SoapObject rpc  =   new  SoapObject(NAMESPACE, METHOD_NAME);
19             //  rpc.addProperty("theCityName", cityName);
20
21             AndroidHttpTransport ht  =   new  AndroidHttpTransport(URL);
22             ht.debug  =   true ;
23
24             SoapSerializationEnvelope envelope  =   new  SoapSerializationEnvelope(
25                     SoapEnvelope.VER11);
26             
27             envelope.bodyOut  =  rpc;
28             envelope.dotNet  =   true ;
29             envelope.setOutputSoapObject(rpc);
30
31             ht.call(SOAP_ACTION, envelope);
32             
33 //             debug(LOG_TAG, "DUMP>> " + ht.requestDump);
34 //             debug(LOG_TAG, "DUMP<< " + ht.responseDump);
35
36             SoapObject result  =  (SoapObject) envelope.bodyIn;
37             Vector vrct  =  (Vector) result.getProperty( " sayHelloResult " );
38              for ( int  i  =   0 ; i  <  vrct.size();i ++ ) {
39                 System.out.println( vrct.get(i));
40             }

41             String ss  =   "" ;
42 //            for(String s : res){
43 //                ss += s;
44 //            }
45            
46             return  ss;
47         }
  catch  (Exception e)  {
48             e.printStackTrace();
49         }

50          return   null ;
51     }

52
53 }

54
55


    (2) 建好后,写一个main方法调用它就可以了。

最后,ksoap2非常的小,一百多K吧,使用它我们可以在Android程序中方便的调用WebService,而不用加载那些庞大的jar包。
最后给一个ksoap2 for android的下载地址

注意:将扩展名改为jar即可!

你可能感兴趣的:(apache,tomcat,android,webservice,SOAP)