移动端以[{'key1':'value1'},{'key2':'value2'},{'key3':'value3'}]传过来的字符串参数,以@Param("::list") List xxx接收易于处理还是@Param("list") String xxx易于处理
这样的话将map转换成实体类就比较麻烦了,不能自动做实体类的匹配,发出请求的源码如下:
HashMap<String, String> resultHash = new HashMap<String, String>();
List<HashMap<String,String>> arr = new ArrayList<HashMap<String,String>> ;
for (int i = 0; i < list.size(); i++) {
hash.put("extendInfo", StringUtil.trim(workDiary.getExtendInfo()));
hash.put("safeInfo", StringUtil.trim(workDiary.getSafeInfo()));
hash.put("weather", StringUtil.trim(workDiary.getWeather()));
hash.put("modifytime", StringUtil.trim(workDiary.getLocalModifyTime()));
arr.put(hash);
}
resultHash.put("list", arr);
PCHttpUtils.httpPost(url, resultHash, Constants.CHARSET, true);
参数接收如下:
@At("/insertOrReplaceList")
public Object insertOrReplaceList(@Param("::list") List list) {
// 判断登录状态
Results result = loginService.chargeAppType();
if (!result.getCode().equals(Results.SUCCESS)) {
return result;
}
String appType = Mvcs.getReq().getHeader("app_type");
StringBuffer idsSb = new StringBuffer("-1");
if (list != null && list.size() > 0) {
for (WorkDiary obj : list) {
obj.setExtend1(appType);
idsSb.append("," + obj.getId());
if (Utils.isNullOrEmpty(obj.getId())) {
return Results.parse(Results.FAIL, "您传递的信息在您本地不存在");
}
}
. ......
要疯了,加了适配器以后一直报错,进不了方法了java.lang.NumberFormatException
at java.math.BigDecimal.(Unknown Source)
at java.math.BigDecimal.(Unknown Source)
at org.nutz.json.impl.JsonTokenScan.readObject(JsonCompileImplV2.java:282)
at org.nutz.json.impl.JsonTokenScan.read(JsonCompileImplV2.java:351)
at org.nutz.json.impl.JsonCompileImplV2.parse(JsonCompileImplV2.java:25)
at org.nutz.json.Json.fromJson(Json.java:40)
at org.nutz.mvc.adaptor.JsonAdaptor.getReferObject(JsonAdaptor.java:35)
请求:
public static String httpPostNew(String url, HashMap<String, Object> params, String charset, boolean encrypt) {
String result = "";
Request req = Request.create(url, METHOD.POST);
for (String key:params.keySet()) {
req.getParams().put(key, params.get(key));
}
req.setEnc(charset);
Header header=req.getHeader();
header.set("Content-Type","application/json");
req.setHeader(header);
Response resp = new FilePostSender(req).setTimeout(5 * 60 * 1000)
.send();
if (resp.isOK()) {
result = resp.getContent();
}
return result;
}
安卓端代码:
JSONArray arr = new JSONArray();
// List<HashMap<String,String>> arr = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < list.size(); i++) {
WorkRecord workrecord = list.get(i);
String type = StringUtil.trim(workrecord.getLocalModifyState()).toUpperCase();
HashMap<String, String> hash = new HashMap<String, String>();
hash.put("id", StringUtil.trim(workrecord.getId()));
hash.put("proId", StringUtil.trim(workrecord.getProId()));
hash.put("recordTitle", StringUtil.trim(workrecord.getRecordTitle()));
hash.put("recordType", StringUtil.trim(workrecord.getRecordType()));
arr.put(hash);
}
resultHash.put("list", arr);
// resultHash.put("list", arr.toString());
AuthUtil.setAuthNew(resultHash, userInfo.getId(), SecretUtil.decrypt(userInfo.getKey()),
userInfo.getUserName());
String s = PCHttpUtils.httpPostNew(url, resultHash, Constants.CHARSET, true);
logFlag = s;