今天又是枯燥的一天,依然敲着代码。
客户有个微信消息推送的需求,找了下官方文档
微信消息推送文档
大致看了一下,需要模板ID和微信后台的小卡片参数名。
随即便敲起了代码,首先定义模板类,代码如下:
public class Template {
private String touser;
private String template_id;
private String page;
private List<TemplateParam> templateParamList;
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getTemplate_id() {
return template_id;
}
public void setTemplate_id(String template_id) {
this.template_id = template_id;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
public String toJSON() {
StringBuffer buffer = new StringBuffer();
buffer.append("{");
buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");
buffer.append(String.format("\"template_id\":\"%s\"", this.template_id)).append(",");
buffer.append(String.format("\"page\":\"%s\"", this.page)).append(",");
buffer.append("\"data\":{");
TemplateParam param = null;
for (int i = 0; i < this.templateParamList.size(); i++) {
param = templateParamList.get(i);
// 判断是否追加逗号
if (i < this.templateParamList.size() - 1){
buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", param.getKey(), param.getValue()));
}else{
buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", param.getKey(), param.getValue()));
}
}
buffer.append("}");
buffer.append("}");
return buffer.toString();
}
public List<TemplateParam> getTemplateParamList() {
return templateParamList;
}
public void setTemplateParamList(List<TemplateParam> templateParamList) {
this.templateParamList = templateParamList;
}
定义TemplateParam类(文档中的data),代码如下
public class TemplateParam {
private String key;
private String value;
public TemplateParam(String key,String value){
this.key=key;
this.value=value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
接口请求方法,如下(需要的包自行查找,我这里就不发出来了):
public static String postData(String urlStr, String data, String contentType) {
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(CONNECT_TIMEOUT);
if (contentType != null) conn.setRequestProperty("content-type", contentType);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
if (data == null) data = "";
writer.write(data);
writer.flush();
writer.close();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
return sb.toString();
} catch (IOException e) {
//logger.error("Error connecting to " + urlStr + ": " + e.getMessage());
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
}
} return null;
}
万事俱备,开始写测试方法
Template template=new Template();
template.setTemplate_id("你的模板ID");
template.setTouser("需要推送给哪位靓仔的openid");
List<TemplateParam> paras=new ArrayList<>();
paras.add(new TemplateParam("小卡片上的参数1","values1"));
paras.add(new TemplateParam("小卡片上的参数2","values2"));
paras.add(new TemplateParam("小卡片上的参数3","values3"));
paras.add(new TemplateParam("小卡片上的参数4","values3"));
template.setTemplateParamList(paras);
String s1 = HttpUtil.postData("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN", template.toJSON());
ACCESS_TOKEN这个值请自行替换哦!
OK,代码已经完毕,开始测试!蹦~哦豁,炸了,报的什么错呢?
{"errcode":47003,"errmsg":"argument invalid! data.小卡片上的参数2.value i}
随即我去文档上面看了一下
47003 模板参数不准确,可能为空或者不满足规则,errmsg会提示具体是哪个字段出错
说我参数不准确?随即我便查看了后台的小卡面参数,认真对照了几遍发现还是报错。
淦!丢雷罗木,我直接复制行了吧!但是事与愿违,还是报错。
既然这样的话,那就是我的参数可能为空或不满足规则了。
空不至于,应该是不满足规则。但是规则是什么呢?
然后我又去耐心的看了一遍文档。看到了【订阅消息参数值内容限制说明】这个东西。
嘿嘿,媛来如此。
报错原因解析:
time.DATA 时间 24小时制时间格式(支持+年月日),支持填时间段,两个时间点之间用“~”符号连接 例如:15:01,或:2019年10月1日 15:01
由于我的参数是时间,但是传的是
2019年10月1日 15时01分01秒
正确的参数应该为
2019年10月1日 15:01:01
大家伙明白了吗?
如果模板参数与微信后台对的上,报错74003的话,就去检查对应的字段是否符合文档的规则。
如果实在是不想检查的话,那我说一个懒人方法
将报错提示字段中的中文都去掉
对了,结尾再多说一句,其实我那个模板代码是复制某位大佬的(小声BB)。
感谢各位靓仔收看本期【low逼程序员的日常生活】,拜了个拜!!!