程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java完成微信平台自界說菜單的創立示例

java完成微信平台自界說菜單的創立示例

編輯:關於JAVA

java完成微信"平台自界說菜單的創立示例。本站提示廣大學習愛好者:(java完成微信"平台自界說菜單的創立示例)文章只能為提供參考,不一定能成為您想要的結果。以下是java完成微信"平台自界說菜單的創立示例正文



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());
 }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved