使用Ksoap连接web services
web services简介:
Web Service是基于网络的、分布式的模块化组件,它执行特定的任务,遵守具体的技术规范,这些规范使得Web Service能与其他兼容的组件进行互操作。Internet Inter-Orb Protocol(IIOP)都已经发布了很长时间了,但是这些模型都依赖于特殊对象模型协议,而 Web Services 利用 SOAP 和 XML对这些模型在通讯方面作了进一步的扩展以消除特殊对象模型的障碍。Web Services 主要利用 HTTP 和 SOAP 协议使商业数据在 Web 上传输,SOAP通过 HTTP 调用商业对象执行远程功能调用,Web 用户能够使用 SOAP 和 HTTP通过 Web 调用的方法来调用远程对象.
摘自: http://baike.baidu.com/view/1086510.htm
soap简介:
SOAP:简单对象访问协议,简单对象访问协议(SOAP)是一种轻量的、简单的、基于 XML 的协议,它被设计成在 WEB 上交换结构化的和固化的信息。 SOAP 可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议( HTTP),简单邮件传输协议(SMTP),多用途网际邮件扩充协议(MIME)。它还支持从消息系统到远程过程调用(RPC)等大量的应用程序。
摘自:http://baike.baidu.com/view/60663.htm
具体使用:
引用jar包文件:
ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
测试代码:
1 public static SoapObject call(String asmxurl, String namespace, String methodname, Map<String, Object> methodPara ){ 2 3 SoapObject so = new SoapObject(namespace, methodname); 4 for(String key : methodPara.keySet()){ 5 so.addProperty(key, methodPara.get(key)); 6 } 7 8 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 9 envelope.bodyOut = so; 10 envelope.dotNet = true; 11 envelope.setOutputSoapObject(so); // 输出对象 12 13 HttpTransportSE transport = new HttpTransportSE(asmxurl); 14 String soapaction = namespace + methodname; 15 try { 16 transport.call(soapaction, envelope); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 SoapObject object = (SoapObject) envelope.bodyIn; // 输入对象 21 return object; 22 }
1 public static void main(String[] args) { 2 // TODO Auto-generated method stub 3 4 String asmxurl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; 5 String namespace = "http://WebXml.com.cn/"; 6 String methodname = "getMobileCodeInfo"; 7 Map<String, Object> methodPara = new HashMap<String, Object>(); 8 methodPara.put("mobileCode", "13888888888"); 9 methodPara.put("userID", ""); 10 SoapObject res = KsoapUtil.call(asmxurl, namespace, 11 methodname,methodPara); 12 for (int i = 0; i < res.getPropertyCount(); i++) { 13 System.out.println("Property-" + i + ": " + res.getProperty(i)); 14 } 15 for (int i = 0; i < res.getAttributeCount(); i++) { 16 System.out.println("Attribute-" + i + ": " + res.getAttribute(i)); 17 } 18 19 }
在调用时的参数介绍:
asmxurl: asmx网址
namespace: 命名空间
methodname: 方法名称
methodpara: 方法参数
其中: 命名空间加方法名称组成soapaction.
其他: 对于空的返回结果(如 <getMobileCodeInfoResult />), 在getProperty(0)时返回anyType{}, 注意判断即可.
浙公网安备 33010602011771号