android soap webservice 数据流传输,Android利用Soap读取WebService并且解析XML的DataSet数据...

一、Soap的结构

06ff34bdd9369431e070064d6ab58b80.png

调用webService需要以下几个参数:命名空间、Soap Action、WSDL的URL、方法名。接下来以调用火车列车信息数据为例,webService地址为:webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx?op=getDetailInfoByTrainCode

二、调用WebService

一般来说,调用webService通常需要几个步骤,在调用之前,我们需要下载Soap的jar包,网上有很多,不再赘述。

1、参数设置:上面说到的几个参数都要先设置,这主要依赖于你要调用的web'Service的网址:

// 命名空间

String nameSpace = "http://WebXml.com.cn/";

// 调用的方法名称

String methodName = "getDetailInfoByTrainCode";

// EndPoint

String endPoint = "http//webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx";

// SOAP Action

String soapAction = "http//WebXml.com.cn/getDetailInfoByTrainCode";

// 命名空间

String nameSpace = "http://WebXml.com.cn/";

// 调用的方法名称

String methodName = "getDetailInfoByTrainCode";

// EndPoint

String endPoint = "http//webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx";

// SOAP Action

String soapAction = "http//WebXml.com.cn/getDetailInfoByTrainCode";

2、指定命名空间与调用方法名

// 指定WebService的命名空间和调用的方法名

SoapObject rpc = newSoapObject(nameSpace, methodName);

// 指定WebService的命名空间和调用的方法名

SoapObject rpc = new SoapObject(nameSpace, methodName);

3、设置参数:

// 设置需调用WebService接口需要传入的两个参数TrainCode、userId

rpc.addProperty("TrainCode", params[0]);

rpc.addProperty("UserID","");

// 设置需调用WebService接口需要传入的两个参数TrainCode、userId

rpc.addProperty("TrainCode", params[0]);

rpc.addProperty("UserID","");

4、生成调用WebService方法的SOAP请求信息

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本

SoapSerializationEnvelope envelope = newSoapSerializationEnvelope(SoapEnvelope.VER12);

envelope.bodyOut = rpc;

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);

envelope.bodyOut = rpc;

5、是否允许调用.Net的WebService

// 设置是否调用的是dotNet开发的WebService

envelope.dotNet = true;

// 等价于envelope.bodyOut = rpc;

envelope.setOutputSoapObject(rpc);

// 设置是否调用的是dotNet开发的WebService

envelope.dotNet = true;

// 等价于envelope.bodyOut = rpc;

envelope.setOutputSoapObject(rpc);

6、创建Http对象

HttpTransportSE transport =newHttpTransportSE(endPoint);

HttpTransportSE transport = new HttpTransportSE(endPoint);

7、调用WebService方法

try{

// 调用WebService

transport.call(soapAction, envelope);

} catch(Exception e) {

e.printStackTrace();

}

try {

// 调用WebService

transport.call(soapAction, envelope);

} catch (Exception e) {

e.printStackTrace();

}

8、返回SoapObject对象

SoapObject object=null;

// 获取返回的数据

if(envelope.bodyIninstanceofSoapFault){

String str=((SoapFault)envelope.bodyIn).faultstring;

System.out.println("2"+str);

}

else{

object = (SoapObject) envelope.bodyIn;

}

SoapObject object=null;

// 获取返回的数据

if(envelope.bodyIn instanceof SoapFault){

String str=((SoapFault)envelope.bodyIn).faultstring;

System.out.println("2"+str);

}

else{

object = (SoapObject) envelope.bodyIn;

}

三、解析WebService中的DataSet数据

由于列车时刻表是DataSet数据,所以我们必须对返回的结果进行解析,在网上查找方法后,自己琢磨终于成功解析,方法如下:

SoapObject soap1=(SoapObject)object.getProperty("getDetailInfoByTrainCodeResult");

SoapObject childs=(SoapObject)soap1.getProperty(1);

SoapObject soap2=(SoapObject)childs.getProperty(0);

///

for(inti=0;i

SoapObject soap3=(SoapObject)soap2.getProperty(i);

///

Info info=newInfo();

info.setStation(soap3.getProperty(0).toString());

info.setArriveTime(soap3.getProperty(1).toString());

info.setStartTime(soap3.getProperty(2).toString());

info.setKm(soap3.getProperty(3).toString());

Raininfo.add(info);

//result=soap3.getProperty(3).toString();

}

SoapObject soap1=(SoapObject)object.getProperty("getDetailInfoByTrainCodeResult");

SoapObject childs=(SoapObject)soap1.getProperty(1);

SoapObject soap2=(SoapObject)childs.getProperty(0);

///

for(int i=0;i

SoapObject soap3=(SoapObject)soap2.getProperty(i);

///

Info info=new Info();

info.setStation(soap3.getProperty(0).toString());

info.setArriveTime(soap3.getProperty(1).toString());

info.setStartTime(soap3.getProperty(2).toString());

info.setKm(soap3.getProperty(3).toString());

Raininfo.add(info);

//result=soap3.getProperty(3).toString();

}

我自己定义了一个Info的model类,用来存放我读取的火车时刻数据,这么多getProperty方法,有可能会看着头晕,接下来接一下xml的截图说明,相信大家一看就会:

3af0c48983c574e05f18e72ce98df0cc.png

PS:如果数据不是DataSet的话,不用这么麻烦,直接就可以利用正则表达式进行解析。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值