一、自定义菜单概述
自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。
二、申请自定义菜单
个人订阅号只能编辑生成菜单,无法开发、企业订阅号通过微信认证;可以申请到自定义菜单资格
服务号默认有菜单权限,企业公众号也有权限。
三、获得AppId 和AppSecert
AppId和AppSecret在开发者中心-开发者ID中,可以找到。
自定义菜单主要代码为:
package weimenu; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import org.json.JSONObject; public class MenuUtil { private static String getAccess_token(){ String APPSECRET=""; //填写自己的AppSecret String APPID="";//填写自己的AppId String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APPID + "&secret=" +APPSECRET; String accessToken = null; try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("GET"); //必须是get方式请求 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒 http.connect(); InputStream is =http.getInputStream(); int size =is.available(); byte[] jsonBytes =new byte[size]; is.read(jsonBytes); String message=new String(jsonBytes,"UTF-8"); JSONObject demoJson = new JSONObject(message); accessToken = demoJson.getString("access_token"); System.out.println(message); } catch (Exception e) { e.printStackTrace(); } return accessToken; } /** * 创建Menu * @Title: createMenu * @Description: 创建Menu * @param @return * @param @throws IOException 设定文件 * @return int 返回类型 * @throws */ public static String createMenu() { String menu = "{\"button\":[" + "{\"name\":\"主页\",\"sub_button\":[" + "{\"type\":\"view\",\"name\":\"主页\",\"url\":\"http://www.baidu.com/\"}," + "{\"type\":\"view\",\"name\":\"微观网\",\"url\":\"http://www.baidu.com/\"}" + "]}," + "{\"name\":\"婚礼人\",\"sub_button\":[" + "{\"type\":\"view\",\"name\":\"结婚\",\"url\":\"http://www.baidu.com/\"}," + "{\"type\":\"view\",\"name\":\"哈哈\",\"url\":\"http://www.baidu.com/\"}" + "]}," + "{\"name\":\"普通用户\",\"sub_button\":[" + "{\"type\":\"view\",\"name\":\"看看\",\"url\":\"http://www.baidu.com/\"}," + "{\"type\":\"click\",\"name\":\"测试\",\"key\":\"WEIFUWU\"}" + "]}" + "]}"; String access_token= getAccess_token(); String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token; try { URL url = new URL(action); HttpURLConnection http =(HttpURLConnection) url.openConnection(); http.setRequestMethod("POST"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒 http.connect(); OutputStream os= http.getOutputStream(); os.write(menu.getBytes("UTF-8"));//传入参数 os.flush(); os.close(); InputStream is =http.getInputStream(); int size =is.available(); byte[] jsonBytes =new byte[size]; is.read(jsonBytes); String message=new String(jsonBytes,"UTF-8"); return "返回信息"+message; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return "createMenu 失败"; } public static void main(String[] args) { System.out.println(createMenu()); } }