在android中使用SOAP与webservice进行数据交互

  因为项目中需要使用SOAP与服务器进行数据的交互,于是做了一个非常简单的例子来熟悉SOAP与WebService间的通信。

   首先需要在项目中导入KSOAP基于android版本的jar包 ksoap2-android-assembly-2.5.4-jar-with-dependencies.jar

   在android中的代码实现:

  
[java]  view plain copy
  1. //1, 指定WebService命名空间 xxxx.com 为你要访问的域名  
  2. String nameSpace = "http://www.xxxx.com/";  
  3.   
  4. //2, 调用的方法名称   
  5. String methodName = "HelloWorld";  
  6.   
  7. //3, EndPoint   
  8.        String endPoint = "http://www.xxxx.com/helloworld.asmx";  
  9.      
  10.        //4, SOAPAction   
  11.        // SOAP Action就是命名空间 + 调用方法的名称  
  12.         String soapAction = "http://www.xxxx.com/HelloWorld";  
  13.   
  14.        // 指定WebService的命名空间和调用的方法名  
  15.         SoapObject rpc = new SoapObject(nameSpace, methodName);  
  16.   
  17.        // 如果有参数,则设置需调用WebService接口需要传入的两个参数  
  18. // 我这里只是返回一个简单HelloWorld所以不要设置参数  
  19. // rpc.addProperty("参数", 值);  
  20.   
  21.   
  22. // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
  23. SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
  24.         
  25. // 设置是否调用的是dotNet开发的WebService   
  26.        // envelope.dotNet = true;  
  27.   
  28. // 等价于  
  29. envelope.bodyOut = rpc;  
  30. envelope.setOutputSoapObject(rpc);  
  31. HttpTransportSE transport = new HttpTransportSE(endPoint);  
  32. try {  
  33.     // 调用WebService  
  34.     transport.call(soapAction, envelope);  
  35.       
  36. catch (Exception e) {  
  37.     e.printStackTrace();  
  38. }  
  39. // 获取返回的数据  
  40. SoapObject object = (SoapObject) envelope.bodyIn;  
  41. // 获取返回的结果 getMobileCodeInfoResult  
  42. String result = object.getProperty("HelloWorldResult").toString();  
  43. // 将WebService返回的结果显示在TextView中  
  44. resultView.setText(result);  


将endPoint也就是请求url后面加上?wsdl在浏览器中访问这个地址,就可以看到如下格式的xml

[html]  view plain copy
  1.  <?xml version="1.0" encoding="utf-8" ?>   
  2. <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.iprecare.com/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://www.iprecare.com/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">  
  3. <wsdl:types>  
  4. <s:schema elementFormDefault="qualified" targetNamespace="http://www.xxxx.com/"> //WebService的命名空间  
  5. <s:element name="HelloWorld"> //调用的方法名称  
  6.   <s:complexType />   
  7.   </s:element>  
  8. <s:element name="HelloWorldResponse"> //调用HelloWorldResponse就会返回HelloWorld   
  9. <s:complexType>  
  10. <s:sequence>  
  11.   <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />   
  12.   </s:sequence>  
  13.   </s:complexType>  
  14.   </s:element>  

SOAP报错:java.lang.RuntimeException: Cannot serialize: 565.0 

                     at org.ksoap2.serialization.SoapSerializationEnvelop.writeElement(SoapSerialization.....

可能原因是:

[java]  view plain copy
  1. rpc.addProperty("参数", 值);  

这里参数的值不能为float,double,网上查了写资料也没有找到为什么会这样。


如果服务器返回数据是 boolean的话,这样获取

[java]  view plain copy
  1. SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();  
  2. boolean    ret = Boolean.parseBoolean(soapPrimitive.toString());  

上传文件关键代码:

[java]  view plain copy
  1.        FileInputStream fis = new FileInputStream(path);  
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  3. byte[] buffer = new byte[1024*15];  
  4. int count = 0;  
  5. while ((count = fis.read(buffer)) >= 0) {   
  6.     baos.write(buffer, 0, count);   
  7. }  
  8. String fs = new String(Base64.encodeBase64(baos .toByteArray())); fis.close();//需要在工程中加入<span style="font-family:Tahoma, Verdana, Simsun, 'Microsoft YaHei', 'Arial Unicode MS', Mingliu, Arial, Helvetica;font-size:13px;LINE-HEIGHT: 19px">commons-codec-1.4.jar</span>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值