加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webService的使用步骤

发布时间:2020-12-17 00:23:09 所属栏目:安全 来源:网络整理
导读:第一:实例化SoapObject 对象,指定webService的命名空间(从相关WSDL文档中可以查看命名空间),以及调用方法名称。如: View Code // 命名空间 private static final String serviceNameSpace = " http://WebXml.com.cn/ " ; // 调用方法(获得支持的城市)

第一:实例化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; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读