搞了兩天的自定義菜單,終於搞定了,現在分享下心得,以便后來者少走彎路......
好了,先看先微信官方的API

官方寫的很詳細,但是我看完后很茫然,不知道你們什么感覺。 我知道是post一個帶參數的請求給url,可是具體怎么發送呢,開始想做一個jsp頁面,使用<form>來發送,可是種種原因不行,所以換種想法,於是有了java get或post訪問url的想法,弄好后一運行,會提示“javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: basic constraints check failed: pathLenConstraint violated - this cert must be the last cert in the certification path”這種錯誤,查詢是證書的問題,在網上百般折騰,后來高人一句話提醒我了(你的url不是公網,服務接入成功了,可以你的開發環境不能),我是在自己電腦上新建的工程,沒有部署到網絡上,你給騰訊發post請求了,可是騰訊接入不到你的本機工程上,所以會出現那個錯誤了。
所以有了下面的結論:把自己新建的工程部署到網絡上,就是你網絡的應用上,才能實現這一功能。不懂的者,參考下我以前的文章“利用微信公眾平台實現自動回復消息——java版”。
進入正題(我以百度雲開發者中心為例):
將自己在百度雲開發者中心中部署的應用中的index.jsp修改如下:
1 <%@page import="java.io.*"%> 2 <%@page import="java.net.*" %> 3 <%@page import="org.json.*" %> 4 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 5 6 <% 7 final String appId = " "; 8 final String appSecret = " "; //自己的APPIP 和APPSECRET 9 10 %> 11 <% 12 class TestGetPost{ 13 14 public String getAccess_token(){ // 獲得ACCESS_TOKEN 15 16 String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" +appSecret; 17 18 String accessToken = null; 19 try { 20 URL urlGet = new URL(url); 21 HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); 22 23 http.setRequestMethod("GET"); //必須是get方式請求 24 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 25 http.setDoOutput(true); 26 http.setDoInput(true); 27 System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//連接超時30秒 28 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時30秒 29 30 http.connect(); 31 32 InputStream is =http.getInputStream(); 33 int size =is.available(); 34 byte[] jsonBytes =new byte[size]; 35 is.read(jsonBytes); 36 String message=new String(jsonBytes,"UTF-8"); 37 38 JSONObject demoJson = new JSONObject(message); 39 accessToken = demoJson.getString("access_token"); 40 41 System.out.println(message); 42 } catch (Exception e) { 43 e.printStackTrace(); 44 } 45 return accessToken; 46 } 47 public int createMenu() throws IOException { 48 String user_define_menu = "{\"button\":[{\"type\":\"click\",\"name\":\"項目管理\",\"key\":\"20_PROMANAGE\"},{\"type\":\"click\",\"name\":\"機構運作\",\"key\":\"30_ORGANIZATION\"},{\"name\":\"日常工作\",\"sub_button\":[{\"type\":\"click\",\"name\":\"待辦工單\",\"key\":\"01_WAITING\"},{\"type\":\"click\",\"name\":\"已辦工單\",\"key\":\"02_FINISH\"},{\"type\":\"click\",\"name\":\"我的工單\",\"key\":\"03_MYJOB\"},{\"type\":\"click\",\"name\":\"公告消息箱\",\"key\":\"04_MESSAGEBOX\"},{\"type\":\"click\",\"name\":\"簽到\",\"key\":\"05_SIGN\"}]}]}"; 49 //此處改為自己想要的結構體,替換即可 50 String access_token= getAccess_token(); 51 52 String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token; 53 try { 54 URL url = new URL(action); 55 HttpURLConnection http = (HttpURLConnection) url.openConnection(); 56 57 http.setRequestMethod("POST"); 58 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 59 http.setDoOutput(true); 60 http.setDoInput(true); 61 System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//連接超時30秒 62 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時30秒 63 64 http.connect(); 65 OutputStream os= http.getOutputStream(); 66 os.write(user_define_menu.getBytes("UTF-8"));//傳入參數 67 os.flush(); 68 os.close(); 69 70 InputStream is =http.getInputStream(); 71 int size =is.available(); 72 byte[] jsonBytes =new byte[size]; 73 is.read(jsonBytes); 74 String message=new String(jsonBytes,"UTF-8"); 75 System.out.println(message); 76 } catch (MalformedURLException e) { 77 e.printStackTrace(); 78 } catch (IOException e) { 79 e.printStackTrace(); 80 } 81 return 0; 82 } 83 }%> 84 <% 85 TestGetPost tgp = new TestGetPost(); 86 87 tgp.createMenu(); 88 %>
部署好后,直接在瀏覽器地址欄中輸入自己百度雲開發者中心下改應用的當前域名即可,然后關注微信公眾賬號看效果(已關注的,取消關注並重新關注,即可看到效果)。
記得將應用中的xml文件里的
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
創建后效果為:

小提示:
1、自定義菜單的查看:直接在瀏覽器地址欄中輸入
https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN (換成自己的access_token,access_token在一段時間里是有效的,所以獲得后,在粘貼到此是沒有問題的)
2、自定義菜單的刪除:
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN (刪除與查看同理)
