遇到了模板消息空白,犯了最蠢的一种错误。
测试号,新增测试模板的时候
{{first.DATA}}
商品:{{product.DATA}}
下单时间:{{time.DATA}}
消费金额:{{amount.DATA}}
{{remark.DATA}}
直接复制粘贴微信开发文档里的模板样式的时候,每行后面都会有空格,需要删掉这些空格。
然后代码部分:
public class TemplateMessageManager {
// 发送模板消息
@Test
public void sendTemplateMessage() {
String at = WxService.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+ at;
String data ="{\"touser\":\"o6Ycf1Hij7X-arhIyo7qIN30R3UM\",\"template_id\":\"aec2hROP0Og-ByBDm08mXftXJHYPB6AsAhXEhqcAiRw\",\"url\":\"http://weixin.qq.com/download\","+
"\"data\":{\"first\":{\"value\":\"恭喜你购买成功!\",\"color\":\"#173177\"},\"product\":{\"value\":\"巧克力\",\"color\":\"#173177\"},\"time\":{\"value\":\"2014年9月22日\",\"color\":\"#173177\"},\"amount\":{\"value\":\"1\",\"color\":\"#173177\"},\"remark\":{\"value\":\"欢迎再次购买!\",\"color\":\"#173177\"}}}";
String result = Util.post(url, data);
System.out.println(result);
}
}
把String data=后面的转义字符串中的空格全删了,然后问题解决了。