1.需要一个ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar的架包。
2.需要知道webservice的命名空间
// WSDL文档中的命名空间
String targetNameSpace = "http://tempuri.org/";
3.需要WSDL文档中的URL
// WSDL文档中的URL
String WSDL = "http://60.190.224.118:5031/XRHotel_ws?wsdl";
4.完整代码
new Thread(new Runnable() { @Override
public void run() {
// WSDL文档中的命名空间
String targetNameSpace = "http://tempuri.org/";
// WSDL文档中的URL
String WSDL = "http://60.190.224.110:5031/XRHotel_ws?wsdl";
// 需要调用的方法名
String getSupportProvince = "GuestInfo"; // 根据命名空间和方法得到SoapObject对象
SoapObject soapObject = new SoapObject(targetNameSpace,
getSupportProvince);
soapObject.addProperty("Name","张三");
soapObject.addProperty("Pwd",null);
soapObject.addProperty("Roomno","");
// 通过SOAP1.1协议得到envelop对象
SoapSerializationEnvelope envelop = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 将soapObject对象设置为envelop对象,传出消息
envelop.bodyOut = soapObject;
// 设置是否调用的是dotNet开发的WebService //指定webservice的类型的(java,PHP,dotNet)
envelop.dotNet = true;
// 等价于envelop.bodyOut = soapObject;
envelop.setOutputSoapObject(soapObject);
HttpTransportSE httpSE = new HttpTransportSE(WSDL);
// 开始调用远程方法
try {
httpSE.call(targetNameSpace + getSupportProvince, envelop);
// 得到远程方法返回的SOAP对象
SoapObject resultObj = (SoapObject) envelop.bodyIn;
// 得到服务器传回的数据
int count = resultObj.getPropertyCount();
Log.i("----------------TAG", "count="+count+";"+resultObj.getProperty().toString());
for (int i = ; i < count; i++) {
Map<String,String> listItem = new HashMap<String, String>();
listItem.put("province", resultObj.getProperty(i).toString());
Log.i("----------------TAG", resultObj.getProperty(i).toString());
listItems.add(listItem);
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}).start();
}
Soap 产生的连接没必要自己去关闭, httpSE.call方法内,在通过流获取数据之后,已经把流关闭了。