手機上需要向服務器傳遞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類要記得序列化