java微信开发自定义菜单

/**
 * 
 */
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");  
    }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值