做JAVA开发微信平台接口时
代码开发完成的时候,满心欢喜开始测试接口的时候,微信给了我欢迎提示
{"errcode":47001,"errmsg":"data format error hint: [VlcJia0987ge21]"}
卧槽一脸闷逼 :数据格式错误?
根据官网的接口参数提示所有数据必须是JSON格式
{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"data":{
"first": {
"value":"恭喜你购买成功!",
"color":"#173177"
},
"keyword1":{
"value":"巧克力",
"color":"#173177"
},
"remark":{
"value":"欢迎再次购买!",
"color":"#173177"
}
}
}
各位码农兄dei这个的意思是全部必须是JSON格式不仅是外面,data里的数据也是标准的JSON格式!
测试传递的参数代码为 DataMap=> Map
@Test
public void testMessageTemplateSend()
{
DataMap params=new DataMap();
params.put("uriName", "MessageTemplateSend");
params.put("touser", "owOP0jrqF9qG3E9qncGpQQQWiHAk");//to lyn
params.put("template_id","LtaQFkeBJTcNTK34nJHfMAlbe3LzFWdlXaeUpK1037Q");
params.put("url","XXXXX/login");
JSONObject templateData = new JSONObject();
JSONObject first = new JSONObject();
first.put("value", "测试消费123");first.put("color", "#FF0000");
templateData.put("first",first.toJSONString());
JSONObject keyword1 = new JSONObject();
keyword1.put("value", "9845754");keyword1.put("color", "#FF0000");
templateData.put("keyword1",keyword1.toJSONString());
JSONObject keyword2 = new JSONObject();
keyword2.put("value", "120.3");keyword2.put("color", "#FF0000");
templateData.put("keyword2",keyword2.toJSONString());
JSONObject keyword3 = new JSONObject();
keyword3.put("value", DateUtil.formatYMDHMSDate(new Date()));keyword3.put("color", "#FF0000");
templateData.put("keyword3", keyword3.toJSONString());
JSONObject keyword4 = new JSONObject();
keyword4.put("value", "XXXX");keyword4.put("color", "#FF0000");
templateData.put("keyword4", keyword4.toJSONString());
JSONObject remark = new JSONObject();
remark.put("value", "有疑问拨打电话");remark.put("color", "#FF0000");
templateData.put("remark",remark.toJSONString());
params.put("data", templateData.toJSONString());
String url = this.getUrl("/api_request_by_uriname.json");
String post = HttpUtil.post(url, params, 3000);
System.out.println(post);
}
后端代码修改为(仅参数部分):
@Override
public String getArgsJSONStr(HttpServletRequest request) {
JSONObject args = new JSONObject();
args.put("touser", request.getParameter("touser"));
args.put("template_id", request.getParameter("template_id"));
args.put("url", request.getParameter("url"));
//得到的关于DATA的参数数据转化成JSON
JSONObject data = JSON.parseObject(request.getParameter("data"));
//把每个详细数据转化成JSON,fisrt,keyword1,keyword2....,remark
Set keySet = data.keySet();
for(String key:keySet)
{
//吧具体数据转化成JSON然后重新放回去
JSONObject keyvalue = JSON.parseObject(data.get(key).toString());
data.put(key,keyvalue);
}
args.put("data", data);
//最后整体数据进行转化为JSON格式用于传递给微信使用
String argsJSONStr = JSON.toJSONString(args);
return argsJSONStr;
}
生成数据为:
{"data":{"first":{"color":"#FF0000","value":"测试消费123"},"keyword1":{"color":"#FF0000","value":"9845754"},"keyword2":{"color":"#FF0000","value":"120.3"},"keyword3":{"color":"#FF0000","value":"2018-05-17 22:08:14"},"keyword4":{"color":"#FF0000","value":"XXXX"},"remark":{"color":"#FF0000","value":"有疑问拨打电话"}},"template_id":"LtaQFkeBJTcNTK34nJHfMAlbe3LzFWdlXaeUpK1037Q","touser":"owOP0jrqF9qG3E9qncGpQQQWiHAk","url":"XXXXX/login"}
发给微信服务器然后给我返回成功,普大喜奔。哈哈哈哈(如果帮希望各位官老爷解决了问题右上角个人资料下面微信二维码打赏1块大洋吧)