KSoap2 Android 是 Android 平台上一个高效、轻量级的 SOAP 开发包。等同于 Android 上的 KSoap2 的移植版本。所以可以独立出客户端进行开发测试工作。
两个组件
1 KSOAP2 客户端(Android2适用)
2 Cxf2.6 java服务端
Cxf2.6下载地址: http://www.apache.org/dyn/closer.cgi?path=/cxf/2.6.6/apache-cxf-2.6.6.zip
主要描述是 如何使用KSOAP调用cxf发布的web服务
java服务端
暴露服务接口
- @WebService(targetNamespace="http://www.cnc.com",name="UserService")
- public interface UserService {
- /* @WebResult(name = "return", targetNamespace = "http://www.cnc.com")
- @Action(input = "urn:sayHello", output = "sayHelloResponse")
- @RequestWrapper(localName = "sayHello", targetNamespace = "http://www.cnc.com")
- @WebMethod(action = "urn:sayHello")
- @ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://www.cnc.com")*/
- public String sayHello( @WebParam(name = "args0", targetNamespace = "http://www.cnc.com")
- java.lang.String args0
- );
- }
服务接口实现类
- @WebService(endpointInterface="com.cncsoft.provide.service.UserService",targetNamespace="http://www.cnc.com")
- public class UserServiceBean implements UserService{
- public String sayHello(String args0) {
- return "This is sayHello First " + args0;
- }
- }
CXF服务发布类
- public class PubWebService {
- protected PubWebService() throws Exception{
- UserServiceBean helloWorld = new UserServiceBean();
- String address = "http://192.168.1.71:9000/cxf";
- //Endpoint.publish(address, helloWorld);
- //发布服务
- JaxWsServerFactoryBean server = new JaxWsServerFactoryBean();
- server.setServiceClass(helloWorld.getClass());
- server.setAddress(address);
- server.create();
- }
- public static void main(String[] args) throws Exception{
- new PubWebService();
- }
- }
KSOAP2 客户端调用
- public class KsoapClient {
- public static void main(String[] args) throws IOException, XmlPullParserException {
- SoapObject request = new SoapObject("http://www.cnc.com", "sayHello");
- request.addProperty("args0","z");
- //生成调用WebService方法的SOAP请求信息
- SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
- envelope.bodyOut = request;
- //创建HttpTransportSE对象,指定WebService的WSDL文档的URL
- HttpTransportSE ht = new HttpTransportSE("http://192.168.1.71:9000/cxf?wsdl");
- //使用call方法调用WebService方法
- ht.call(null, envelope);
- //使用getResponse方法获得WebService方法的返回结果
- Object soapObject = envelope.getResponse();
- System.out.println(soapObject);
- }
- }
KsoapClient中request.addProperty("参数名",传递的值);
参数名必须与Web服务端发布的WSDL文件中的方法参数同名