微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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; }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐