顺晟科技
2021-06-16 11:00:03
318
导入Java。io。IOexception
导入Java。io。InputStream
导入Java。io。OutputStream
导入Java。net。httpurlconnection
导入Java。net。异常异常;
导入Java。net。URL
导入org。JSON。jsonobject
公共类MenuUtil {
/**
* 获得ACCESS_TOKEN
* @Title: getAccess_token
* @描述:获得ACCESS_TOKEN
* @param @return设定文件
* @返回字符串返回类型
* @抛出
*/
私有静态字符串getAccess_token(){
字符串APPID=
字符串APPSECRET=
字符串网址='https://api.weixin.qq.com/cgi-bin/token?grant _ type=client _ credential APPID=' APPID ' secret=' APPSECRET;
字符串accessToken=null
尝试{
URL urlGet=新URL(URL);
http purl连接http=(Httpurl连接)urlGet。OpenConnection();
http。setRequestMethod(' GET ');//必须是得到方式请求
http。setRequestProperty(' Content-Type ',' application/x-www-form-URL encoded ');
http。SetDooutput(true);
http。SetDoinPut(true);
系统。set属性(' sun。net。客户。DefaultConnectTimeout ',' 30000 ');//连接超时30秒
系统。SetProperty(' sun。net。客户。DefaultReadTimeout ',' 30000 ');//读取超时30秒
http。connect();
InputStream为=http。GetinputStream();
int size=is。available();
byte[]JSonBytes=new byte[size];
是。read(JSonBytes);
字符串消息=新字符串(jsonBytes,‘UTF-8’);
Json对象demoJson=新JSON对象(消息);
访问令牌=Demojson。getStrIng(' access _ token ');
System.out.println(消息);
}捕获(例外e) {
e .printstacktrace();
}
返回访问令牌
}
/**
* 创建菜单
* @Title: createMenu
* @描述:创建菜单
* @param @return
* @ param @抛出异常设定文件
* @return int返回类型
* @抛出
*/
公共静态字符串createMenu() {
字符串菜单=' { \ ' button ' :[{ \ ' type ' : ' click \ ',\ ' name ' : ' MENU01 \ ',\ ' key \ ' : \ ' 1 \ ' },{ \ ' type ' : ' click \ ',\ ' name ' : \ '天气查询\ ',\ '键' :\ '西安\'},{ \ ' name \ ' : \ '日常工作\ ',\ ' sub _ button ' :[{ \ ' type ' : ' click \ ',\ 名称' : \ '待办工单\ ',\ ' key \ ' : \ ' 01 _ WAITING \ ' },{ \ ' type \ ' : \ ' click \ ',\ ' name \ ' : \ '已办工单\ ',\ ' key \ ' : \ ' 02 _ FINISH \ ' },{ \ ' type \ ' : \ ' click \ ',\ ' name \ ' : \ '我的工单\ ',\ '键' : \ 03 _ MYJOB \ ' },{ \ '键入' : '单击\ ',\ '名称' : \ '公告消息箱\ ',\ ' key \ ' : \ ' 04 _ MESSAGEBOX \ ' },{ \ ' type \ ' : \ ' click \ ',\ ' name \ ' : \ '签到\ ',\ ' key ' : ' 05 _ SIGN \ ' }]} ';
//此处改为自己想要的结构体,替换即可
string access _ token=GetACCESS _ token();
弦乐动作='https://api.weixin.qq.com/cgi-bin/menu/create?access _ token=' access _ token
尝试{
URL url=新URL(动作);
http purl connection http=(Httpurl connection)URL。OpenConnection();
http。setRequestMethod(' POST ');
http。setRequestProperty(' Content-Type ',' application/x-www-form-URL encoded ');
http。SetDooutput(true);
http。SetDoinPut(true);
系统。set属性(' sun。net。客户。DefaultConnectTimeout ',' 30000 ');//连接超时30秒
系统。SetProperty(' sun。net。客户。DefaultReadTimeout ',' 30000 ');//读取超时30秒
http。connect();
输出流OS=http。get output stream();
OS。write(菜单。getbytes(' UTF-8 ');//传入参数
OS。flush();
OS。close();
InputStream为=http。GetinputStream();
int size=is。available();
byte[]JSonBytes=new byte[size];
是。read(JSonBytes);
字符串消息=新字符串(jsonBytes,‘UTF-8’);
"返回"返回信息消息;
} catch(Maformedurlexception e){
e .printstacktrace();
} catch (IOException e) {
e .printstacktrace();
}
返回"创建菜单"失败;
}
/**
* 删除当前菜单
* @Title:删除菜单
* @描述:删除当前菜单
* @param @return设定文件
* @返回字符串返回类型
* @抛出
*/
公共静态字符串删除菜单()
{
string access _ token=GetACCESS _ token();
字符串操作=' https://API。微信。QQ。com/CGI-bin/menu/delete?access _ token=' access _ token
尝试{
URL url=新URL(动作);
http purl connection http=(Httpurl connection)URL。OpenConnection();
http。setRequestMethod(' GET ');
http。setRequestProperty(' Content-Type ',' application/x-www-form-URL encoded ');
http。SetDooutput(true);
http。SetDoinPut(true);
系统。set属性(' sun。net。客户。DefaultConnectTimeout ',' 30000 ');//连接超时30秒
系统。SetProperty(' sun。net。客户。DefaultReadTimeout ',' 30000 ');//读取超时30秒
http。connect();
输出流OS=http。get output stream();
OS。flush();
OS。close();
InputStream为=http。GetinputStream();
int size=is。available();
byte[]JSonBytes=new byte[size];
是。read(JSonBytes);
字符串消息=新字符串(jsonBytes,‘UTF-8’);
返回"删除菜单"返回信息' : '消息;
} catch(Maformedurlexception e){
e .printstacktrace();
} catch (IOException e) {
e .printstacktrace();
}
返回"删除菜单"失败;
}
公共静态void main(String[] args) {
系统。出去。println(CreateMenu());
}
}
17
2022-03
28
2021-08
03
2021-08
16
2021-06
16
2021-06
16
2021-06