java微信开发自定义菜单
来源:互联网 发布:linux 压缩文件夹 命令 编辑:程序博客网 时间:2024/05/21 10:24
/** * */package com.caiyl.zmd.weixin;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;public class WeixinMenuUtils {// http客户端 private static HttpClient httpclient; static { httpclient = HttpClientBuilder.create().build(); } public static void main(String[] args) { try { // 获取accessToken -参数appid,secret String accessToken = getAccessToken("appid", "secret"); System.out.println(accessToken); // 创建菜单 // String s = "{\"button\":[{\"name\":\"休闲娱乐\",\"sub_button\":[{\"type\":\"click\",\"name\":\"笑话大全\",\"key\":\"m_joke\"},{\"type\":\"click\",\"name\":\"内涵段子\",\"key\":\"m_duanzi\"},{\"type\":\"click\",\"name\":\"爆笑图片\",\"key\":\"m_laughImg\"}]},{\"name\":\"实用工具\",\"sub_button\":[{\"type\":\"click\",\"name\":\"天气查询\",\"key\":\"m_weather\"},{\"type\":\"click\",\"name\":\"公交查询\",\"key\":\"m_bus\"},{\"type\":\"click\",\"name\":\"功能菜单\",\"key\":\"m_sysmenu\"}]},{\"name\":\"消息示例\",\"sub_button\":[{\"type\":\"click\",\"name\":\"关于企特\",\"key\":\"m_about\"},{\"type\":\"click\",\"name\":\"图文消息\",\"key\":\"m_imgmsg\"},{\"type\":\"click\",\"name\":\"音乐消息\",\"key\":\"m_musicmsg\"}]}]}"; // String res = createMenu(s, accessToken); // System.out.println(res); // 查询菜单 String info = getMenuInfo(accessToken); System.out.println(info); } catch (Exception e) { e.printStackTrace(); } } /** * 获取accessToken */ public static String getAccessToken(String appid, String secret) throws Exception { HttpGet get=new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret+""); HttpResponse response = httpclient.execute(get); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject object = JSON.parseObject(jsonStr); return object.getString("access_token"); } /** * 创建菜单 */ public static String createMenu(String params, String accessToken) throws Exception { HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken); httpost.setEntity(new StringEntity(params, "UTF-8")); HttpResponse response = httpclient.execute(httpost); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(jsonStr); JSONObject object = JSON.parseObject(jsonStr); return object.getString("errmsg"); } /** * 查询菜单 */ public static String getMenuInfo(String accessToken) throws Exception { HttpGet get = new HttpGet("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken); HttpResponse response = httpclient.execute(get); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); return jsonStr; } /** * 删除自定义菜单 */ public static String deleteMenu(String accessToken) throws Exception { HttpGet get = new HttpGet("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken); HttpResponse response = httpclient.execute(get); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject object = JSON.parseObject(jsonStr); return object.getString("errmsg"); } }
0 0
- java微信开发自定义菜单
- 微信开发 -- 自定义菜单
- 微信开发-自定义菜单
- 微信开发--自定义菜单
- 微信自定义菜单开发
- 微信开发自定义菜单
- JAVA微信自定义菜单
- Java微信公众平台开发之自定义菜单
- JAVA微信公众号开发之自定义菜单
- Java企业微信开发_04_自定义菜单
- Java之微信开发自定义菜单的实现
- 微信公众开放平台开发08---纯java 实现微信开发:编写自定义菜单
- java 微信自定义菜单 java微信接口开发 公众平台 SSM
- 微信自定义菜单开发报错
- php微信自定义菜单开发
- 微信公众平台自定义菜单开发
- 微信开发(二)自定义菜单
- 微信开发二三事:自定义菜单
- day19
- 教你自己实现一个事件总线EventBus
- Single Number
- 如何申请百度ak
- HDU 5372 Segment Game
- java微信开发自定义菜单
- Download Manager (HDU3233,UVA12231,UVALive4486) 解题报告
- Java 多线程实现的两种方式
- HDU5375 Gray code
- iOS项目开发实战——iOS网络编程获取网页Html源代码
- 带分区的UITableView的创建(省市区字典)
- 窗口与视图UIScreen and UIView
- (3.1.4.2)struts1和struts2
- HDU 5373 The shortest problem