当前位置: 移动技术网 > IT编程>开发语言>Java > java实现微信公众平台自定义菜单的创建示例

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网