国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實(shí)現(xiàn)微信公眾平臺(tái)自定義菜單的創(chuàng)建示例

2019-11-26 15:34:15
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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    設(shè)定文件
 * @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方式請(qǐng)求   
             http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");   
             http.setDoOutput(true);       
             http.setDoInput(true);
             System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//連接超時(shí)30秒
             System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時(shí)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;
     }

 /**
  * 創(chuàng)建Menu
 * @Title: createMenu
 * @Description: 創(chuàng)建Menu
 * @param @return
 * @param @throws IOException    設(shè)定文件
 * @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/"}]}]}";

        //此處改為自己想要的結(jié)構(gòu)體,替換即可
        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");//連接超時(shí)30秒
           System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時(shí)30秒

           http.connect();
           OutputStream os= http.getOutputStream();   
           os.write(menu.getBytes("UTF-8"));//傳入?yún)?shù)   
           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 失敗";
   }

    /**
     * 刪除當(dāng)前Menu
    * @Title: deleteMenu
    * @Description: 刪除當(dāng)前Menu
    * @param @return    設(shè)定文件
    * @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");//連接超時(shí)30秒
          System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時(shí)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());
 }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新邵县| 长武县| 汤原县| 扶风县| 红桥区| 九江县| 冷水江市| 皋兰县| 凌海市| 临汾市| 托克逊县| 垣曲县| 镇安县| 九龙县| 肥西县| 卓尼县| 鹰潭市| 盐边县| 松阳县| 日土县| 孝昌县| 察雅县| 洛隆县| 孝义市| 同江市| 涿鹿县| 鄂托克前旗| 四川省| 衡南县| 长寿区| 武功县| 安远县| 阿拉尔市| 平原县| 阿拉尔市| 通榆县| 吉木萨尔县| 芜湖县| 项城市| 华蓥市| 章丘市|