这是我在应用程序中调用 Web 服务方法的方式。请注意我用来转换 Java 日期的方法。您需要 ISO 日期格式。
protected static Object callMethod(String method, Map<String, Object> parameters) throws IOException, XmlPullParserException {
SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE, method);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.headerOut = new Element[1];
envelope.headerOut[0] = buildAuthHeader();
envelope.setOutputSoapObject(request);
if (parameters != null) {
for (String item : parameters.keySet()) {
Object itemValue = parameters.get(item);
if (itemValue.getClass().getName().equals("java.util.Date")) {
// If it's a date then we have to format it because ksoap
// does not know how to do this.
request.addProperty(item, getSOAPDateString((java.util.Date) itemValue));
} else {
request.addProperty(item, itemValue);
}
}
}
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS, 10000);
httpTransport.debug = true;
httpTransport.call(WSDL_TARGET_NAMESPACE + "/" + method, envelope);
String soapString = httpTransport.requestDump;
System.out.println(soapString);
return envelope.getResponse();
}
这是返回实际字符串的方法:
private static Object getSOAPDateString(java.util.Date itemValue) {
String lFormatTemplate = "yyyy-MM-dd'T'hh:mm:ss'Z'";
DateFormat lDateFormat = new SimpleDateFormat(lFormatTemplate);
String lDate = lDateFormat.format(itemValue);
return lDate;
}