栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现微信公众平台自定义菜单的创建示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 {

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

 
    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 失败";
   }

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152309.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号