main函数:
public class DeleteMenu {
public static void main(String[] args) {
//地址
String url = " https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";
url = url.replace("ACCESS_TOKEN",WxService.getAccessToken());
Util util = new Util();
String result = util.get(url);
System.out.println("删除菜单的结果:"+result);
}
}
WxService.getAccessToken所在部分的函数:(获取AccessToken)
private static final String Token = "ouyang";
private static final String Get_Token_Url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static final String appID = "你的appid";
private static final String appsecret = "你的appsecret";
private static AccessToken at;
private static void getToken() {
String url = Get_Token_Url.replace("APPID",appID).replace("APPSECRET",appsecret);
String tokenStr = Util.get(url);
System.out.println(tokenStr);
JSONObject jsonObj = JSONObject.parseObject(tokenStr);
String token = jsonObj.getString("access_token");
String expireIn = jsonObj.getString("expires_in");
at = new AccessToken(token,expireIn);
}
public static String getAccessToken() {
if(at == null || at.isExpired()) {
getToken();
}
return at.getAccessToken();
}
AccessToken类:
public class AccessToken {
private String accessToken;
private long expireTime;
public AccessToken(String accessToken,String expireIn) {
super();
this.accessToken = accessToken;
expireTime = System.currentTimeMillis()+Integer.parseInt(expireIn)*1000;
}
/* Token是否过期 */
public boolean isExpired() {
return System.currentTimeMillis()>expireTime;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public long getExpireTime() {
return expireTime;
}
public void setExpireTime(long expireTime) {
this.expireTime = expireTime;
}
}
如有不对,欢迎斧正。