手机上需要向服务器传递List对象,搞了半天才成功,不然就只能传递数组对象,或者将对象拼成字符串传递了。
不多说了,上代码吧。
手机端:
public void addTime(List<SetTime> addTimes, String loginName,
String license, String loginID, String password) {
if (NetWorkUtil.checkNetWork(context)) {
RequestParams params = new RequestParams();
for (int i = 0; i < addTimes.size(); i++) {
params.put("setTimes[" + i + "].type", addTimes.get(i)
.getType());
params.put("setTimes[" + i + "].week", addTimes.get(i)
.getWeek());
params.put("setTimes[" + i + "].startTime", addTimes.get(i)
.getStartTime());
params.put("setTimes[" + i + "].endTime", addTimes.get(i)
.getEndTime());
params.put("setTimes[" + i + "].interval", addTimes.get(i)
.getInterval());
}
params.put("license", license);
params.add("loginID", loginID);
params.add("password", password);
params.add("loginName", loginName);
RequstClient.post(HttpUrl.settime, params,
new LoadCacheResponseLoginouthandler(context,
new LoadDatahandler() {
@Override
public void onStart() {
super.onStart();
//System.out.println("开始拉去数据");
}
@Override
public void onSuccess(byte[] content) {
super.onSuccess(content);
addmsg = new String(content);
switch (addmsg) {
case "YES":
addmsg = "设置命令已经下发";
break;
case "NO":
addmsg = "抱歉,设置命令未能下发";
break;
default:
addmsg = "抱歉,设置命令未能下发";
break;
}
Message msg = new Message();
msg.what = WhatUtil.ADDTIME;
handler.sendMessage(msg);
}
@Override
public void onFailure(String error,
String message) {
super.onFailure(error, message);
Toast.makeText(context,
"抱歉网络连接超时,请重新检查网络设置", 1).show();
//System.out.println("错误的数据" + message);
}
@Override
public void onFinish() {
super.onFinish();
}
}));
} else {
NetWorkUtil.openDialog(context);
}
}
客户端传递参数的时候需要分别将List中的对象的属性一个个注入,否则服务端就会报异常。
服务端:
@RequestMapping(value = "/app_downset.action", method = RequestMethod.POST
@ResponseBody
public String appDownSet(@RequestParam String license,
@RequestParam String loginName, @RequestParam String loginID,
@RequestParam String password, @ModelAttribute SetTimes setTimes) {
System.out.println(setTimes.getSetTimes().get(0).getType());
SpiltCom spiltCom = new SpiltCom();
if (loginIdDao.checkUser(loginID, password)) {
license = license.trim();
loginName = loginName.trim();
loginID = loginID.trim();
password = password.trim();
String carAllInfo = vehicleDao.getCarAllInfo(license);
if (carAllInfo != null && carAllInfo != "") {
String[] split = carAllInfo.split(",");
String bodystr = "";
for (SetTime setTime : setTimes.getSetTimes()) {
String body = spiltCom.initSpiltCom(setTime);
bodystr = bodystr + body;
}
int size = setTimes.getSetTimes().size();
String length = "";
if (size < 16) {
length = "0" + Integer.toHexString(size);
} else {
length = Integer.toHexString(size);
}
bodystr = length + bodystr;
String MsgBody = getSet(split[3], XWatchUtil.hex2byte(bodystr));
boolean downSet = loginIdDao.downSet(split[0], split[1],
split[2], split[3], "8103", loginName, MsgBody,
"设置定位间隔");
if (downSet) {
return "YES";
} else {
return "NO";
}
} else {
return null;
}
} else {
return null;
}
}
服务端的SeTimes即为List的封装类
客户端和服务端的setTime类要记得序列化