微信公众自定义菜单5个字或5个字以上

如果你现在在做微信公众号开发,好吧,你看到这篇文章应该不是如果在做了,相信你已经蛋疼了一段时间了。先来张微信后台萌萌的截图


微信后台很负责的告诉你,自定义菜单第一级目录汉字不能超过4个;但是或许你不是很走运,遇到一个比较较真的老板或产品经理,然后他给你一张截图,如下


你没有看错,顿时心里一万只草泥马在奔腾,好吧!THE END !  来点正片。。。

上代码 



public class WechatsUtils {
	
	
 
	 
	private static String createMuneUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
	private static String ACCESS_TOKEN = "GYjAv47glwSZ_99323fosljGofew029CNSNNNFOOJSOOJOJOFJOSdsBP1Rh2k49ynEeo0WtSAGFFIl47aRafKuKzhG2-vMJLaAJAQNP";
	
	private static String delMuneUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
	public static String getWechatsToken() throws IOException{
		HttpURLConnection httpConn = null;
		String requesturl = "https://api.weixin.qq.com/cgi-bin/token";
		String appid = "wx2604e00000ac35b";
		String appsecret = "32ab4f4513awed000f1daa71979cd6e3";
		requesturl += "?grant_type=client_credential&appid="+appid;
		requesturl += "&secret="+appsecret;
		URL url = new URL(requesturl);
		httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		httpConn.setUseCaches(false);
		httpConn.setRequestProperty("Content-Type", "GET");
		httpConn.connect();
		OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
		out.flush();
		out.close();
		//接收返回
		BufferedReader bf = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
		String line;
		StringBuffer sb = new StringBuffer();
		while ((line = bf.readLine()) != null) {
			sb.append(line);
		}
		bf.close();
		httpConn.disconnect();
		String result = new String(sb);
		System.out.println("返回输出:"+result);
		return result;
	}
	public static String createMune() throws Exception{
		JSONObject json = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject js = new JSONObject();
		js.put("name", "超过5个字菜单1");
		js.put("type", "view");
		js.put("url", "http://www.nimengognsidedizhi.com/index.html");
		ja.add(js);
		JSONObject js1 = new JSONObject();
		js1.put("name", "超过5个字菜单2");
		js1.put("type", "view");
		js1.put("url", "http://www.nimengognsidedizhi.com/index.html");
		ja.add(js1);
		JSONObject js2 = new JSONObject();
		js2.put("name", "更多");
		
		
		JSONArray jsub2 = new JSONArray();
		JSONObject jsonsub2 = new JSONObject();
		jsonsub2.put("type", "view");
		jsonsub2.put("name", "二级子菜单1");
		jsonsub2.put("url", "http://www.nimengognsidedizhi.com/index.html");
		jsub2.add(jsonsub2);
		
		JSONObject jsonsub3 = new JSONObject();
		jsonsub3.put("type", "view");
		jsonsub3.put("name", "二级子菜单2");
		jsonsub3.put("url", "http://www.nimengognsidedizhi.com/index.html");
		jsub2.add(jsonsub3);
		
		JSONObject jsonsub4 = new JSONObject();
		jsonsub4.put("type", "view");
		jsonsub4.put("name", "二级3");
		jsonsub4.put("url", "http://www.nimengognsidedizhi.com/index.html");
		jsub2.add(jsonsub4);
		
		JSONObject jsonsub5 = new JSONObject();
		jsonsub5.put("type", "view");
		jsonsub5.put("name", "二级子菜单2");
		jsonsub5.put("url", "http://www.nimengognsidedizhi.com/index.html");
		jsub2.add(jsonsub5);
		
		
		js2.put("sub_button", jsub2);
		
		
		
		ja.add(js2);
		json.put("button", ja);
		HttpURLConnection httpConn = null;
		String pathUrl = createMuneUrl+ACCESS_TOKEN;
		URL url = new URL(pathUrl);
		httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		httpConn.setUseCaches(false);
		httpConn.setRequestProperty("Content-Type", "GET");
		httpConn.connect();
		OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
		out.write(new String(json.toString().getBytes("UTF-8")));
		out.flush();
		out.close();
		//接收返回
		BufferedReader bf = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
		String line;
		StringBuffer sb = new StringBuffer();
		while ((line = bf.readLine()) != null) {
			sb.append(line);
		}
		bf.close();
		httpConn.disconnect();
		String result = new String(sb);
		System.out.println("返回输出:"+result);
		return result;
	}
	
	public static void delMuneUrl() throws IOException{
		HttpURLConnection httpConn = null;
		String pathUrl = delMuneUrl+ACCESS_TOKEN;
		URL url = new URL(pathUrl);
		httpConn = (HttpURLConnection) url.openConnection();
		httpConn.setDoOutput(true);
		httpConn.setDoInput(true);
		httpConn.setUseCaches(false);
		httpConn.setRequestProperty("Content-Type", "GET");
		httpConn.connect();
		OutputStreamWriter out = new OutputStreamWriter(httpConn.getOutputStream());
		out.flush();
		out.close();
		//接收返回
		BufferedReader bf = new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
		String line;
		StringBuffer sb = new StringBuffer();
		while ((line = bf.readLine()) != null) {
			sb.append(line);
		}
		bf.close();
		httpConn.disconnect();
		String result = new String(sb);
		System.out.println("返回输出:"+result);
	}
	public static void main(String[] args) throws Exception{
//		WechatsUtils.getWechatsToken();
		WechatsUtils.delMuneUrl();
		WechatsUtils.createMune();
	}
}


请把微信秘钥换成你们自己的,很明显哥们我写出来的秘钥是参考用的,当然是在真实的秘钥上面改了些参数

然后,你需要先调用 getWechatsToken获取微信token 之后进行操作;delMuneUrl是啥子意思了?其实就是把微信自带的菜单干掉,然后crateMune创建自己的自定义菜单,也就是

你只需要运行这个main方法一次, 微信的菜单里面的内容就随之改变了。。。。 OK 。。。 扯到这你了,祝你成功












评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值