java實現微信公眾平台自訂菜單的建立樣本_java

來源:互聯網
上載者:User

複製代碼 代碼如下:

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 {

 /**
  * 獲得ACCESS_TOKEN
 * @Title: getAccess_token
 * @Description: 獲得ACCESS_TOKEN
 * @param @return    設定檔案
 * @return String    傳回型別
 * @throws
  */
 private static String getAccess_token(){ 

  String APPID="";
  String APPSECRET="";

       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\":[{\"type\":\"click\",\"name\":\"MENU01\",\"key\":\"1\"},{\"type\":\"click\",\"name\":\"天氣查詢\",\"key\":\"西安\"},{\"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\"}]}]}";

        //此處改為自己想要的結構體,替換即可
        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 失敗";
   }

    /**
     * 刪除當前Menu
    * @Title: deleteMenu
    * @Description: 刪除當前Menu
    * @param @return    設定檔案
    * @return String    傳回型別
    * @throws
     */
   public static String deleteMenu()
   {
       String access_token= getAccess_token();
       String action = "https://api.weixin.qq.com/cgi-bin/menu/delete? access_token="+access_token;
       try {
          URL url = new URL(action);
          HttpURLConnection http =   (HttpURLConnection) url.openConnection();   

          http.setRequestMethod("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();
          OutputStream os= http.getOutputStream();   
          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 "deleteMenu返回資訊:"+message;
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
       return "deleteMenu 失敗";  
   }
 public static void main(String[] args) {

  System.out.println(createMenu());
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.