微信消息订阅报{"errcode":47001,"errmsg":"data format error hint: [VlcJia0987ge21]"} java代码
微信消息订阅遇到的一些坑
首先看微信官方提供的说明文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/subscribe-message.html
详细内容请看文档说明
简要说明如下

服务器代码:
/** * 生成模板消息 * @param openid * @param msg */ public static void templateMsg(String openid,String msg) { String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + CommonUtil.getAccessToken(); try { JSONObject map = new JSONObject(); map.put("touser", openid); map.put("template_id", "G5Z3j8fLSBReDmnOHemFoyCA_6qHGWojdOMsz0UXEQ4");
//developer 开发板;trial 测试版;formal为正式版;默认正式版, 上线要改成formal map.put("miniprogram_state","trial"); JSONObject data = new JSONObject(); String time = DateFormatUtils.format(new Date(), "yyyy年MM月dd日 HH:mm"); data.put("thing3",new ValueVo(msg)); data.put("time4", new ValueVo(time)); map.put("data", data); String jsonStr = httpsRequest(url, "POST", map.toJSONString()); JSONObject json = JSONObject.parseObject(jsonStr); System.err.println("打印"+json); } catch (Exception e) { e.printStackTrace(); System.err.println("消息订阅异常!"); } } public static String httpsRequest(String requestUrl, String requestMethod, String output) throws Exception { URL url = new URL(null, requestUrl, new Handler()); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod(requestMethod); if (null != output) { OutputStream outputStream = connection.getOutputStream(); outputStream.write(output.getBytes("UTF-8")); outputStream.close(); } // 从输入流读取返回内容 InputStream inputStream = connection.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; StringBuffer buffer = new StringBuffer(); while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); inputStream.close(); inputStream = null; connection.disconnect(); return buffer.toString(); } public class ValueVo { private String value; public ValueVo(String value) { this.value = value; } }
打印{"errcode":0,"errmsg":"ok","msgid":1838082200404099077}
微信返回结果
本文来自博客园,作者:lanwf,转载请注明原文链接:https://www.cnblogs.com/lccsdncnblogs/p/subscribe-message.html

浙公网安备 33010602011771号