Android进价篇-SOAP协议
使用SOAP协议的前提是你必须下载一个KSOAP包:ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar包
然后操作的第一步:
实例化SoapObject对象:
1.serviceNameSpace 命令空间
2.methodName 调用方法
SoapObject request=new SoapObject(serviceNameSpace, methodName);
第二步:
设置SOAP请求信息(参数部分为SOAP协议版本号,与你要调用的webService中版本号一致)
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
第四步:
注册Envelope
(new MarshalBase64()).register(envelope);
第五步:
构建传输对象,并指明WSDL文档URL
String serviceURL = "http://webservice.WebXml.com.cn/WebServices/WeatherWS.asmx"; AndroidHttpTransport ht = new AndroidHttpTransport(serviceURL); ht.debug = true;
第六步:
调用WebService(其中参数为1:命名空间+方法名称,2:Envelope对象)
ht.call(serviceNameSpace+methodName, envelope);
实例代码:
SoapObject detail = null; try { SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); rpc.addProperty("theCityName", cityName); AndroidHttpTransport ht = new AndroidHttpTransport(URL); ht.debug = true; SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc); ht.call(SOAP_ACTION, envelope); SoapObject result = (SoapObject) envelope.bodyIn; detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult"); } catch (Exception e) { e.printStackTrace(); } return detail;