调用微信模版消息实例
步骤 :
1: 去微信公众号申请 模版消息功能 。
2:进入模版消息 ,从模板库中添加一个模版
3:点击详情查看格式
基本准备工作已经就绪 ,接下来就是开发过程
// 开发者ID(AppID)
String appid = “XXXXXXXXX”;
// 开发者密码
String app_secret = “XXXXXXXXXXXX”;
// 获取access_token 的url
String access_token_url = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=”
+ appid + “&secret=” + app_secret;
String access_token = null;
JSONObject jsonObject = null;
InputStream is = new URL(access_token_url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName(“UTF-8”)));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
String jsonText = sb.toString();
jsonObject = JSONObject.fromObject(jsonText);
} finally {
is.close();
}
if (null != jsonObject) {
access_token = jsonObject.getString(“access_token”);
// 发送模版消息数据的url
String url = “https://api.weixin.qq.com/cgi-bin/message/template/send? access_token=” + access_token;
// 请求包为一个json:(这里是用了类转化json的方法 创建的json 也可以指直接拼接字符串)
// String body = “{\”touser\”:
// \”XXXXX\”,\”template_id\”:
// \”XXXXX\”,\”url\”:
// \”http://www.anquan100.com\”,\”data\”:
// {\”keyword1\”:{\”value\”:\”GuestChan\”,\”color\”:\”#173177\”},\”keyword3\”:{\”value\”:\”400\”,\”color\”:\”#173177\”}}}”;
TemplateMessage t = new TemplateMessage();
TemplateMessageData td = new TemplateMessageData();
td.setFirst(new TemplateMessageValue(“亲耐滴芮碧特”, “#173177”));
td.setRemark(new TemplateMessageValue(“点击“详情”确认本次缴费信息”, “#173177”));
td.setKeyword1(new TemplateMessageValue(“远程教育收费”, “#173177”));
td.setKeyword2(new TemplateMessageValue(“888.88”, “#173177”));
td.setKeyword3(new TemplateMessageValue(“666.66”, “#173177”));
td.setKeyword4(new TemplateMessageValue(“111.11”, “#173177”));
td.setKeyword5(new TemplateMessageValue(“111.11”, “#173177”));
t.setTemplate_id(“XXXXXXX”);// 模版id
t.setTouser(“XXXXXX”);// 用户的openid
t.setUrl(“http://www.anquan100.com”); // 用户点击详情打开的地址
t.setData(td);
JSONObject json = JSONObject.fromObject(t);
CloseableHttpClient httpClient = null;
CloseableHttpResponse response = null;
String result = “”;
try {
// 通过址默认配置创建一个httpClient实例
httpClient = HttpClients.createDefault();
// 创建httpGet远程连接实例
HttpPost httpGet = new HttpPost(url);
// 设置请求头信息
httpGet.setHeader(“Content-Type”, “application/json”);
httpGet.setEntity(new StringEntity(json.toString(), “utf-8”));
// 设置配置请求参数
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
.setConnectionRequestTimeout(35000)// 请求超时时间
.setSocketTimeout(60000)// 数据读取超时时间
.build();
// 为httpGet实例设置配置
httpGet.setConfig(requestConfig);
// 执行get请求得到返回对象
response = httpClient.execute(httpGet);
// 通过返回对象获取返回数据
HttpEntity entity = response.getEntity();
// 通过EntityUtils中的toString方法将结果转换为字符串
result = EntityUtils.toString(entity);
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
TemplateMessageData(实体类)
private TemplateMessageValue first; //头信息
private TemplateMessageValue remark; //尾信息
private TemplateMessageValue keyword1;
private TemplateMessageValue keyword2;
private TemplateMessageValue keyword3;
private TemplateMessageValue keyword4;
private TemplateMessageValue keyword5;
TemplateMessageValue(实体类)
public TemplateMessageValue() {
}
public TemplateMessageValue(String value,String color)
{
this.value = value;
this.color = color;
}
private String value;
private String color;
直接运行就可以接收到消息啦