第一:实例化SoapObject 对象,指定webService的命名空间(从相关WSDL文档中可以查看命名空间),以及调用方法名称。如:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
// 命名空间 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中版本号一致):
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
// 获得序列化的Envelope SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.bodyOut = request;
第四步:注册Envelope,
(new MarshalBase64()).register(envelope);
第五步:构建传输对象,并指明WSDL文档URL:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
// 请求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对象):
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
transport.call(serviceNameSpace + getWeatherbyCityName, envelope);
第七步:解析返回数据:
![](http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
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; }