近日在项目中写了个OPhone 调用Webservice的工具类,主要是使用了KSoap2来实现的,代码如下: package com.ictehi.ophone.util; import java.io.IOException; import java.util.Iterator; import java.util.Map; import org.ksoap2.SoapEnvelope; import org.ksoap2.SoapFault; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.AndroidHttpTransport; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.util.Log; public class WSUtils { public WSUtils() { } /** * @author Eric.Lu * @param endPoint * @param methodName * @param params * @param wsdl * @return SoapObject */ public static SoapObject callWS(String nameSpace, String methodName, Map params, String wsdl) { final String SOAP_ACTION=nameSpace+methodName; SoapObject request = new SoapObject(nameSpace, methodName); SoapObject soapResult=null; if(params !=null && !params.isEmpty()){ for(Iterator it=params.entrySet().iterator();it.hasNext();){//遍历MAP Map.Entry<String, Object> e=(Map.Entry<String, Object>) it.next(); request.addProperty(e.getKey().toString(),e.getValue()); } } /** * 设置Soap版本 * 类型:VER1.0,VER1.1,VER1.2 */ // SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER10);//SOAP 1.0 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);// SOAP 1.1 // SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER12);//SOAP 1.2 envelope.dotNet=true;//是否是dotNet WebService envelope.bodyOut = request; // HttpTransportSE ht = new HttpTransportSE(wsdl); AndroidHttpTransport androidHT=new AndroidHttpTransport(wsdl); try { // ht.call(SOAP_ACTION, envelope); androidHT.call(SOAP_ACTION,envelope); } catch (IOException e) { Log.e("IOException:", e.getMessage()); } catch (XmlPullParserException e1) { Log.e("XmlPullParserException", e1.getMessage()); } try { soapResult=(SoapObject)envelope.getResponse(); } catch (SoapFault e) { Log.e("SoapFault",e.getMessage()); } return soapResult; } } 调用这个工具类的方法如下: HashMap<String, Object> params = new HashMap<String, Object>(); params.put("theCityName", peoples[position]); String wsdl = "webservice的wsdl地址"; String nameSpace = "wsdl中的namespace"; String methodName = "方法名";// // SoapObject result = WSUtils.callWS(nameSpace, methodName, // wsdl,peoples[position]); SoapObject result = WSUtils.callWS(nameSpace, methodName, params, wsdl); 附 KSoap2 android版本下载:下载地址