600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > java实现微信创建菜单_java实现微信公众平台自定义菜单的创建示例

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

时间:2021-03-26 16:57:39

相关推荐

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

import java.io.ioexception;

import java.io.inputstream;

import java.io.outputstream;

import .httpurlconnection;

import .malformedurlexception;

import .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./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(".client.defaultconnecttimeout", "30000");//连接超时30秒

system.setproperty(".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./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(".client.defaultconnecttimeout", "30000");//连接超时30秒

system.setproperty(".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./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(".client.defaultconnecttimeout", "30000");//连接超时30秒

system.setproperty(".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());

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。