微信公众自定义菜单5个字或5个字以上
来源:互联网 发布:xp磁盘碎片整理软件 编辑:程序博客网 时间:2024/06/07 06:37
如果你现在在做微信公众号开发,好吧,你看到这篇文章应该不是如果在做了,相信你已经蛋疼了一段时间了。先来张微信后台萌萌的截图
微信后台很负责的告诉你,自定义菜单第一级目录汉字不能超过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 。。。 扯到这你了,祝你成功
阅读全文
0 0
- 微信公众自定义菜单5个字或5个字以上
- 微信公众平台-自定义菜单接口
- 微信公众平台自定义菜单
- 微信公众平台 创建自定义菜单
- PHP微信公众平台自定义菜单
- 微信公众平台自定义菜单开发
- 微信公众号之自定义菜单
- 微信公众平台自定义菜单
- 微信公众平台创建自定义菜单
- 微信公众平台开发自定义菜单
- 微信公众平台自定义菜单
- 微信公众平台开发-自定义菜单
- 微信公众平台 自定义菜单
- 微信公众号开发--自定义菜单
- 微信公众号添加自定义菜单
- 微信公众号自定义菜单
- 微信公众号自定义菜单
- 微信公众平台设置自定义菜单
- WF(工作流)模块介绍
- 蓝桥杯 ALGO-70 算法训练 最长字符串
- Glide图片框架使用详细介绍(三)自定义glide以及图片处理自定义transform
- 逆向工程核心原理读书笔记-API钩取之计算器显示中文数字
- WEKA使用教程(经典教程转载)
- 微信公众自定义菜单5个字或5个字以上
- 【脚本语言系列】关于Python基础知识设计模式,你需要知道的事
- 干货!2017苹果开发者大会发布了啥_看这篇就够了
- 逆向工程核心原理读书笔记-API钩取之隐藏进程(一)
- 蓝桥杯 ALGO-64 算法训练 大小写判断
- WPF Application启动界面设置——
- C#之八大排序算法
- Ecshop订单状态数据库字段(order_status,shipping_status,pay_status)
- 【ionic】做一个简单的 card功能