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