MVC传递List对象

手机上需要向服务器传递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类要记得序列化




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值