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

 


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

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

   在android中的代码实现:

  
		//1, 指定WebService命名空间 xxxx.com 为你要访问的域名
		String nameSpace = "http://www.xxxx.com/";
		
		//2, 调用的方法名称 
		String methodName = "HelloWorld";
		
		//3, EndPoint 
	        String endPoint = "http://www.xxxx.com/helloworld.asmx";
	    
	        //4, SOAPAction 
	        // SOAP Action就是命名空间 + 调用方法的名称
	         String soapAction = "http://www.xxxx.com/HelloWorld";
		
	        // 指定WebService的命名空间和调用的方法名
	         SoapObject rpc = new SoapObject(nameSpace, methodName);
		
	        // 如果有参数,则设置需调用WebService接口需要传入的两个参数
		// 我这里只是返回一个简单HelloWorld所以不要设置参数
		// rpc.addProperty("参数", 值);
	
		
		// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        
		// 设置是否调用的是dotNet开发的WebService 
	        // envelope.dotNet = true;
	
		// 等价于
		envelope.bodyOut = rpc;
		envelope.setOutputSoapObject(rpc);
		HttpTransportSE transport = new HttpTransportSE(endPoint);
		try {
			// 调用WebService
			transport.call(soapAction, envelope);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 获取返回的数据
		SoapObject object = (SoapObject) envelope.bodyIn;
		// 获取返回的结果 getMobileCodeInfoResult
		String result = object.getProperty("HelloWorldResult").toString();
		// 将WebService返回的结果显示在TextView中
		resultView.setText(result);


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

 <?xml version="1.0" encoding="utf-8" ?> 
- <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/">
- <wsdl:types>
- <s:schema elementFormDefault="qualified" targetNamespace="http://www.xxxx.com/"> //WebService的命名空间
- <s:element name="HelloWorld"> //调用的方法名称
  <s:complexType /> 
  </s:element>
- <s:element name="HelloWorldResponse"> //调用HelloWorldResponse就会返回HelloWorld 
- <s:complexType>
- <s:sequence>
  <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" /> 
  </s:sequence>
  </s:complexType>
  </s:element>

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

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

可能原因是:

 rpc.addProperty("参数", 值);

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


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

SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
boolean    ret = Boolean.parseBoolean(soapPrimitive.toString());

上传文件关键代码:

        FileInputStream fis = new FileInputStream(path);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	byte[] buffer = new byte[1024*15];
	int count = 0;
	while ((count = fis.read(buffer)) >= 0) { 
		baos.write(buffer, 0, count); 
	}
	String fs = new String(Base64.encodeBase64(baos .toByteArray())); fis.close();//需要在工程中加入commons-codec-1.4.jar





 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值