Service:
============================start========================================
服务器架构Spring+xFire
--------------------------------applicationContext-wmsmd.xml--------------------------------
<!-- MD主接口开始 -->
<bean name="VWMSMDWebService" class="org.codehaus.xfire.spring.ServiceBean">
<property name="serviceBean" ref="VWMSMDWebServiceBean"></property>
<property name="serviceClass" value="com.sinoservices.wms.md.MasterDataService"></property>
</bean>
<bean id="VWMSMDWebServiceBean" class="com.sinoservices.wms.md.MasterDataServiceImpl"></bean>
<!-- MD主接口结束 -->
---------------------------------model--------------------------------------------------------------
WMSMD_User_Request{
private String userName;
private String passwd;
}
WMSMD_Message_Response{
// 返回 操作信息
private String message;
//是否 成功 (状态 )
private Boolean success;
//返回操作对象
private Object result;
}
----------------------------Interface----------------------------------------------------------------
@WebService
@SOAPBinding(style=Style.DOCUMENT)
public interface MasterDataService {
@WebMethod
public @WebResult(name="user_response") WMSMD_Message_Response LoginUserInfo(@WebParam(name="request") WMSMD_User_Request request);
@WebMethod
public @WebResult(name="sku_response") String queryStringInfo(@WebParam(name="sku") String sku);
}
--------------------------------implements------------------------------------------------------------
@WebService(endpointInterface = "com.sinoservices.wms.md.MasterDataService")
public class MasterDataService implements MasterDataService {
}
--------------------------------wsdl-------------------------------------------------------------------------
在服务端生成服务会有一个wsdl文件
要注意里面的这个部分
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://model.md.wms.sinoservices.com">
<xsd:complexType name="WMSMD_User_Request">
<xsd:sequence>
<xsd:element minOccurs="0" name="passwd" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="userName" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="WMSMD_Message_Response">
<xsd:sequence>
<xsd:element minOccurs="0" name="message" nillable="true" type="xsd:string"/>
<xsd:element minOccurs="0" name="result" nillable="true" type="xsd:anyType"/>
<xsd:element minOccurs="0" name="success" nillable="true" type="xsd:boolean"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
在建立客户端Model的时候要安照这里的顺序来写.
============================the end======================================
Test Client
============================start========================================
//定义常量
private static final String stringMethodName = "queryStringInfo";
private static final String stringMethodName2 = "LoginUserInfo";
private static final String stringNamespace = "http://md.wms.sinoservices.com";
private static final String stringURL = "http://10.100.0.88/WMS/services/MasterDataService";
public void Ksoap2ObjectClent() {
SoapObject soapObject = new SoapObject(Ksoap2JavaTest.stringNamespace,
Ksoap2JavaTest.stringMethodName2);
//set property
WMSMD_User_Request model = new WMSMD_User_Request();
model.setUserName("kitty");
model.setPasswd("123456");
PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("in0");
propertyInfo.setValue(model);
propertyInfo.setType(WMSMD_User_Request.class);
soapObject.addProperty(propertyInfo);
//end
/**
* 生成调用WebService方法的SOAP请求信息。
* 创建SoapSerializationEnvelope对象时需要通过SoapSerializationEnvelope类的构造方法设置SOAP协议的版本号。
* 该版本号需要根据服务端WebService的版本号设置。
* 在创建SoapSerializationEnvelope对象后,不要忘了设置SOAPSoapSerializationEnvelope类的bodyOut属性,
* 该属性的值就是在第一步创建的SoapObject对象。
*/
SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
soapSerializationEnvelope.dotNet = true;
soapSerializationEnvelope.encodingStyle = "UTF-8";
//set bodyOut
soapSerializationEnvelope.setOutputSoapObject(soapObject);
//add Mapping
soapSerializationEnvelope.addMapping(Ksoap2JavaTest.stringNamespace,WMSMD_User_Request.WMSMD_USER_CLASS.getSimpleName(),WMSMD_User_Request.class);
/**
* 关键地方在这里:是否传递复杂数据对象.
* 如果要使用复杂对象传递数据在这里去register.
* 前面数据封装见 /*set property 部分*/
* 如果是基本数据类型
* soapObject.addProperty("in0", "测试WebService接口.");
*/
new WMSMD_USER_MARSHAL().register(soapSerializationEnvelope);
try {
/**
* 创建HttpTransportsSE对象。
* AndroidHttpTransport ht = new AndroidHttpTransport(URL);
* default timeout:20000
* HttpTransportSE(String url, int timeout){}
*/
HttpTransportSE httpTransportSE = new HttpTransportSE(
Ksoap2JavaTest.stringURL);
httpTransportSE.debug = true;
/**
* 使用call方法调用WebService方法
* <pre>
* 这里的第一个参数根据<wsdlsoap:operation soapAction=""/>
* 中soapAction来决定是否为空。
* </pre>
* 第2个参数就是在第3步创建的SoapSerializationEnvelope对象。
* <pre>
* <p>这里要访问网络还需要权限:AndroidManifest.xml </p>
* <uses-permission android:name="android.permission.INTERNET"></uses-permission>
* </pre>
*/
httpTransportSE.call(null, soapSerializationEnvelope);
/**
* 获得WebService方法的返回结果
*
* 有两种方法:
*
* 1、使用getResponse方法获得返回数据。
* envelope.getResponse();
* 2、使用 bodyIn 及 getProperty。
* SoapObject result = (SoapObject)envelope.bodyIn;
* result.getProperty("XXX");
* /
SoapObject objectResult = (SoapObject) soapSerializationEnvelope.getResponse();
WMSMD_Message_Response response = new WMSMD_Message_Response();
for(int i=0;i<objectResult.getPropertyCount();i++){
response.setProperty(i, objectResult.getProperty(i));
}
System.out.println(JSON.toJSONString(response));
soapSerializationEnvelope.getInfo(WMSMD_Message_Response.class, null);
//WMSMD_Message_Response [] res = (WMSMD_Message_Response [])soapSerializationEnvelope.getInfo(WMSMD_Message_Response.class, null);
} catch (Exception exception) {
exception.printStackTrace();
}
}
-----------------------------------------------client model-------------------------------------------------------------------------------
===========WMSMD_Message_CLASS KvmSerializable=============
public class WMSMD_Message_Response implements KvmSerializable {
private static final int INDEX_MESSAGE = 0;
private static final int INDEX_RESULT = 1;
private static final int INDEX_SUCCESS = 2;
public static Class WMSMD_Message_CLASS = WMSMD_Message_Response.class;
// 返回 操作信息
private String message;
//是否 成功 (状态 )
private Boolean success;
//返回操作对象
private Object result;
//getter and setter
......
/**重写KvmSerializable的方法**/
@Override
public Object getProperty(int index) {
// TODO Auto-generated method stub
Object obj = null;
switch (index) {
case INDEX_MESSAGE:
obj = getMessage();
break;
case INDEX_SUCCESS:
obj = getSuccess();
break;
case INDEX_RESULT:
obj = getResult();
break;
default:
break;
}
return obj;
}
@Override
public int getPropertyCount() {
// TODO Auto-generated method stub
return 3;
}
@Override
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo prop) {
// TODO Auto-generated method stub
switch (index) {
case INDEX_MESSAGE:
//第一种写法
prop.name = "message";
prop.type = PropertyInfo.STRING_CLASS;
prop.setValue(this.message);
break;
case INDEX_SUCCESS:
//第二种写法
prop.setName("success");
prop.setType(PropertyInfo.BOOLEAN_CLASS);
prop.setValue(this.success);
break;
case INDEX_RESULT:
prop.setName("result");
prop.setType(PropertyInfo.OBJECT_CLASS);
prop.setValue(this.result);
break;
default:
break;
}
}
@Override
public void setProperty(int index, Object objVal) {
// TODO Auto-generated method stub
switch (index) {
case INDEX_MESSAGE:
setMessage(objVal.toString());
break;
case INDEX_SUCCESS:
setSuccess(Boolean.parseBoolean(objVal.toString()));
break;
case INDEX_RESULT:
SoapObject objectResult = (SoapObject)objVal;
WMSMD_User_Request response = new WMSMD_User_Request();
for(int i=0;i<objectResult.getPropertyCount();i++){
response.setProperty(i, objectResult.getProperty(i));
}
setResult(response);
break;
default:
break;
}
}
=========Marshal============
public class WMSMD_USER_MARSHAL implements Marshal {
private static String DATACONTRACT = "http://model.md.wms.sinoservices.com";
private static String CLASSNAME = "WMSMD_User_Request";
@Override
public Object readInstance(XmlPullParser parser, String arg1, String arg2,
PropertyInfo arg3) throws IOException, XmlPullParserException {
// TODO Auto-generated method stub
return WMSMD_User_Request.fromString(parser.nextText());
}
@Override
public void register(SoapSerializationEnvelope envelope) {
// TODO Auto-generated method stub
envelope.addMapping(DATACONTRACT, CLASSNAME, WMSMD_User_Request.class, this);
}
@Override
public void writeInstance(XmlSerializer writer, Object obj)
throws IOException {
WMSMD_User_Request user = (WMSMD_User_Request)obj;
writer.startTag(DATACONTRACT, "passwd");
writer.text(user.getPasswd());
writer.endTag(DATACONTRACT, "passwd");
writer.startTag(DATACONTRACT, "userName");
writer.text(user.getUserName());
writer.endTag(DATACONTRACT, "userName");
}
}
===================WMSMD_User_Request KvmSerializable===================
//这里实现方法类比于WMSMD_Message_CLASS
public class WMSMD_User_Request implements KvmSerializable {
private static final int INDEX_USERNAME = 0;
private static final int INDEX_PASSWORD = 1;
public static Class WMSMD_USER_CLASS = WMSMD_User_Request.class;
private String passwd;
private String userName;
.........
}
============================the end======================================
到这里关于在Android 中使用Ksoap2调用WebService的代码测试例子全部写完了.