webService的使用步骤

第一:实例化SoapObject 对象,指定webService的命名空间(从相关WSDL文档中可以查看命名空间),以及调用方法名称。如:

View Code
   
   
// 命名空间 private static final String serviceNameSpace = " http://WebXml.com.cn/ " ; // 调用方法(获得支持的城市) private static final String getSupportCity = " getSupportCity " ; // 实例化SoapObject对象 SoapObject request = new SoapObject(serviceNameSpace, getSupportCity);

第二步:假设方法有参数的话,设置调用方法参数

request.addProperty("参数名称","参数值");

第三步:设置SOAP请求信息(参数部分为SOAP协议版本号,与你要调用的webService中版本号一致):

View Code
   
   
// 获得序列化的Envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = request;

第四步:注册Envelope,

(new MarshalBase64()).register(envelope);

第五步:构建传输对象,并指明WSDL文档URL:

View Code
   
   
// 请求URL private static final String serviceURL = " http://www.webxml.com.cn/webservices/weatherwebservice.asmx " ; // Android传输对象 AndroidHttpTransport transport = new AndroidHttpTransport(serviceURL); transport.debug = true ;

第六步:调用WebService(其中参数为1:命名空间+方法名称,2:Envelope对象):

View Code
   
   
transport.call(serviceNameSpace + getWeatherbyCityName, envelope);

第七步:解析返回数据:

View Code
   
   
if (envelope.getResponse() != null ){ return parse(envelope.bodyIn.toString()); } /** ************ * 解析XML * @param str * @return */ private static List < String > parse(String str){ String temp; List < String > list = new ArrayList < String > (); if (str != null && str.length() > 0 ){ int start = str.indexOf( " string " ); int end = str.lastIndexOf( " ; " ); temp = str.substring(start, end - 3 ); String []test = temp.split( " ; " ); for ( int i = 0 ;i < test.length;i ++ ){ if (i == 0 ){ temp = test[i].substring( 7 ); } else { temp = test[i].substring( 8 ); } int index = temp.indexOf( " , " ); list.add(temp.substring( 0 , index)); } } return list; }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值