package com.spoon.tools; import java.io.IOException; import java.util.Map; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; public class XrWebServiceUtils { private static String SERVICE_NS = "http://tempuri.org/"; private static String SERVICE_URL = "http://58.58.178.173:88/WebService/PPKD.asmx"; public static void setSERVICE_NS(String sERVICE_NS) { SERVICE_NS = sERVICE_NS; } public static void setSERVICE_URL(String sERVICE_URL) { SERVICE_URL = sERVICE_URL; } /** * soapRequest * * value1 : 参数的集合 value2 : 调用的方法 * */ public static String soapRequest(Map<String, Object> map, String method) { // 调用的方法 String methodName = method; // 创建HttpTransportSE传输对象 HttpTransportSE ht = new HttpTransportSE(SERVICE_URL); ht.debug = true; // 使用SOAP1.1协议创建Envelop对象,此对象用于向服务器端传入客户端输入的数据 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); // 实例化SoapObject对象,需要传入所调用Web Service的命名空间和Web Service方法名 SoapObject soapObject = new SoapObject(SERVICE_NS, methodName); // 通过遍历Map集合把参数传给soapObject.addProperty(xx,xx); if (map.size() > 0) { for (Map.Entry<String, Object> entry : map.entrySet()) { soapObject.addProperty(entry.getKey(), entry.getValue()); } } // 将soapObject对象传递给服务器 envelope.bodyOut = soapObject; envelope.dotNet = true; envelope.encodingStyle = "UTF-8"; try { // 调用Web Service ht.call(SERVICE_NS + methodName, envelope); if (envelope.getResponse() != null) { // 获取服务器响应返回的SOAP消息 SoapObject result = (SoapObject) envelope.bodyIn; XrLoguUtil.i("WebServiceUtil", result.getProperty(0) + ""); return result.getProperty(0) + ""; } } catch (IOException e) { e.printStackTrace(); return null; } catch (XmlPullParserException e) { e.printStackTrace(); return null; } return null; } }