1. 将对象序列化后,编成Base64编码,作为String来传输,然后在服务端或客户端再将base64编码还原成对象。
2. 直接传递对象也可,但不能直接转换,只能通过getProperty方法获得对象中属性的值,可以在客户端按属性来赋这些值,也可以建立一个通用的将SoapObject转换成对象的方法。
ksoap2使用第二种方式传递对象,对于对象中包含的字段全部为基本类型,没有问题,如果字段中包含复杂类型或其他自定义类型,则会出错,
根据ksoap2官方文档描述,针对java webservice服务端来说,更倾向使用对象序列化传递参数,而服务端接收参数反序列化,反之亦然。另外实际体会,使用对象序列化传递的是二进制字节流,效果也有所提高。
webservice 服务端使用cxf
接口定义:
@WebMethod
public static boolean updateTtRegularSwitch(@WebParam(name = "tregularSwitch")String regularSwitch) {//regularSwitch 为TtRegularSwitch对象的序列化字符串
try {
byte[] buf = new BASE64Decoder().decodeBuffer(regularSwitch);
Object obj = bytesToObject(buf);
TtRegularSwitch regSwitch = (TtRegularSwitch)obj;
System.out.println(regSwitch.getJobStartTime().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
}
Android 客户端ksoap2调用
class UpdateObjectAsyncTask extends AsyncTask{
String result = "";
@Override
protected Object doInBackground(Object... params)
{
try
{
TtRegularSwitch s1= new TtRegularSwitch();
s1.setId(new Long(1));
s1.setInspectPersons("dfsdfds");
s1.setJobName("定期切换");
s1.setJobStartTime(new Date());
List<TtRegularSwitchItem> items = new ArrayList<TtRegularSwitchItem>();
TtRegularSwitchItem item = new TtRegularSwitchItem();
item.setId(new Long(1));
item.setLinchpinProcess("6)汇控柜内的“控制电源、加热器电源、开关储能电源、线路压变二次电源、信号电源、”均合上,闸刀电动电源拉开。母线压变二次计量电源、母线压变二次测量电源、母线压变二次保护电源均合上");
item.setItemNo(1);
items.add(item);
item = new TtRegularSwitchItem();
item.setId(new Long(1));
item.setLinchpinProcess("5)汇控柜内的“控制电源、加热器电源、开关储能电源、线路压变二次电源、信号电源、”均合上,闸刀电动电源拉开。母线压变二次计量电源、母线压变二次测量电源、母线压变二次保护电源均合上");
item.setItemNo(2);
items.add(item);
s1.setSwitchItems(items);
byte [] tre = objectToBytes(s1);
String tregularSwitch = new String(org.apache.commons.codec.binary.Base64.encodeBase64(tre));
String serviceIp = DemoAppActivity.getProps().getProperty("xmppHost");
String serviceUrl = "http://"+serviceIp+":7070/Androidpn/webservice/systemClient?wsdl";
String methodName = "updateTtRegularSwitch";
SoapObject request = new SoapObject("http://ws.znpt.wh.com/",methodName);
request.addProperty("tregularSwitch", tregularSwitch);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.bodyOut = request;
HttpTransportSE ht = new HttpTransportSE(serviceUrl);
ht.call(null, envelope);
if (envelope.bodyIn != null)
{
SoapObject soapObject = (SoapObject) envelope.bodyIn;
result = soapObject.getPropertyAsString("return");
}
else
{
result = "-----服务器未响应------";
}
}
catch (Exception e)
{
result = "-----调用WebService错误------";
}
//异步线程中对View组件更新,必须重新开启线程
valiResultView.post(new Runnable() {
@Override
public void run() {
valiResultView.setText(result);
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
return null;
}
}
public static byte[] objectToBytes(Object obj) throws Exception
{
// logger.debug("objectToString called ");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream sOut = new ObjectOutputStream(out);
sOut.writeObject(obj);
sOut.flush();
byte[] bytes = out.toByteArray();
// logger.debug(bytes.toString());
return bytes;
}
webservice 服务端和客户端 PO实体
服务端PO
package com.wh.znpt.tt.inspect.model;
public class TtRegularSwitch extends BaseObject {
/**
*
*/
private static final long serialVersionUID = -7606368051709196674L;
private Long id; //编号
private Long categoryId; //分类编号
private Long borrowId; //终端领用编号
private String jobName=""; //作业名称
private String jobClassGroup=""; //作业班组
private String leadPerson=""; //负责人
private String inspectPersons=""; //检查人
private Date jobStartTime; //作业开始时间
private Date jobEndTime; //作业结束时间
private List<TtRegularSwitchItem> switchItems;
get()、set() //略
}
public class TtRegularSwitchItem extends BaseObject {
/**
*
*/
private static final long serialVersionUID = 2922908080382687707L;
private Long id; //编号
private Long switchId; //切换记录单编号
private Integer itemNo; //序号
private String linchpinProcess=""; //关键工序
private String standardSteps=""; //标准及步骤
private String riskRemind=""; //风险提醒
private String inspectResult=""; //检查结果
}
客户端PO:
直接复制服务端PO,
注意事项:服务端、客户端的PO对象如果要正常序列化和反序列化,必须满足以下三个条件
1、实现Serializable接口
2、具备相同的serialVersionUID
3、完全相同的包名+类名