android代码
/**
* <b>function:</b>
* @author Xu Ming
* @createDate 2011-11-28 下午05:27:08
* @file TEst.java
* @package com.xph.bms.ws
* @project bms
* @blog http://blog.csdn.net/xuming9
* @email xuming9@163.com
* @version 1.0
* @Description: TODO
*/
package com.xph.bms.ws;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class GetCurTime {
private static final String NAMESPACE = "http://serivice.aa.com";
private static String URL = "http://192.168.1.43:8088/AA/HelloWorld?wsdl=HelloWorld.wsdl";
private static final String METHOD_NAME2 = "sayHi";
private static String SOAP_ACTION = NAMESPACE + "/" + METHOD_NAME2;
public String getCurTimeFromServers()
{
// 第1步:创建SoapObject对象,并指定WebService的命名空间和调用的方法名
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
// 第2步:设置WebService方法的参数
request.addProperty("arg0", "test");
// 第3步:创建SoapSerializationEnvelope对象,并指定WebService的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// 设置bodyOut属性
envelope.bodyOut = request;
// 第4步:创建HttpTransportSE对象,并指定WSDL文档的URL
HttpTransportSE ht = new HttpTransportSE(URL);
try
{
// 第5步:调用WebService
ht.call(SOAP_ACTION, envelope);
if (envelope.getResponse() != null)
{
// 第6步:使用getResponse方法获得WebService方法的返回结果
String result =envelope.getResponse().toString();
System.out.println("result = "+result);
return result;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
WSDL部分代码
<?xml version="1.0" encoding="UTF-8" ?>
<wsdl:definitions name="HelloWorld" targetNamespace="http://serivice.tb.com" xmlns:ns1="http://serivice.tb.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xs:schema elementFormDefault="unqualified" targetNamespace="http://serivice.tb.com" version="1.0" xmlns:tns="http://serivice.tb.com" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="getTime" type="tns:getTime" />
<xs:element name="getTimeResponse" type="tns:getTimeResponse" />
<xs:element name="sayHi" type="tns:sayHi" />
<xs:element name="sayHiResponse" type="tns:sayHiResponse" />
<xs:complexType name="sayHi">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="sayHiResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="getTime">
<xs:sequence />
</xs:complexType>
- <xs:complexType name="getTimeResponse">
- <xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
特别注意(琢磨了好久的):
// 第2步:设置WebService方法的参数
request.addProperty("arg0", "test");
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string" />
</xs:sequence>
代码中的第一个参数为调用方法的参数名称,该名称必须与WSDL中element name相同,否则会报错
SoapFault - faultcode: 'soap:Client' faultstring: 'Unmarshalling Error: unexpected element (uri:"", local:"parameterName"). Expected elements are <{}arg0> ' faultactor: 'null' detail: null