您的位置:首页 > 移动开发 > Android开发

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

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

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

在android中的代码实现:

[java] view
plaincopy

//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

[html] view
plaincopy

<?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.....

可能原因是:

[java] view
plaincopy

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

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

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

[java] view
plaincopy

SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();

boolean ret = Boolean.parseBoolean(soapPrimitive.toString());

上传文件关键代码:

[java] view
plaincopy

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();//需要在工程中加入<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: