3-4_菜单查询与删除

 

自定义菜单查询接口

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141014

在 /Weixin/src/com/imooc/util/WeixinUtil.java

添加一个变量

	private static final String QUERY_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN";

和一个方法:

	public static JSONObject queryMenu(String token){
		String url = QUERY_MENU_URL.replace("ACCESS_TOKEN", token);
		JSONObject jsonObject = doGetStr(url);
		return jsonObject;
	}

 

/Weixin/src/com/imooc/test/WeixinTest.java

package com.imooc.test;

import net.sf.json.JSONObject;

import com.imooc.po.AccessToken;
import com.imooc.util.WeixinUtil;

public class WeixinTest {

	public static void main(String[] args) {
		try {
			AccessToken token = WeixinUtil.getAccessToken();
			System.out.println("票据:"+token.getToken());
			System.out.println("有效时间:"+token.getExpiresIn());
		
//			String path = "G:/imooc.jpg";
//			String mediaId = WeixinUtil.upload(path, token.getToken(), "thumb");
//			System.out.println(mediaId);
			
//			String menu = JSONObject.fromObject(WeixinUtil.initMenu()).toString();
//			int result = WeixinUtil.createMenu(token.getToken(), menu);
//			if(result == 0){
//				System.out.println("菜单创建成果");
//			}else{
//				System.out.println("错误码:"+result);
//			}
			
			// 菜单查询
			JSONObject jsonObject = WeixinUtil.queryMenu(token.getToken());
			System.out.println(jsonObject);
		
		}  catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

输出:

票据:12_v2hGmn4PyKpdDFxNO2exqedAx7PcV9IYZUDScy3PyA2mmIOTSsWUKP6hlUbW0xVZHfCQyMNVz2khDxShi7Q0Iy7Z18GPqK4sj5TvPcHIg8j27YiZk48fec30oqnv8QBdVKLSZ8NoCasYf4ImGLGhAAACHC
有效时间:7200
{"menu":{"button":[{"type":"click","name":"click菜单","key":"11","sub_button":[]},{"type":"view","name":"view菜单","url":"https://www.imooc.com","sub_button":[]},{"name":"菜单","sub_button":[{"type":"scancode_push","name":"扫码","key":"31","sub_button":[]},{"type":"location_select","name":"地理位置","key":"32","sub_button":[]}]}]}}
{"menu":
	{"button":[
		{"type":"click","name":"click菜单","key":"11","sub_button":[]},
		{"type":"view","name":"view菜单","url":"https://www.imooc.com","sub_button":[]},
		{"name":"菜单","sub_button":[
			{"type":"scancode_push","name":"扫码","key":"31","sub_button":[]},
			{"type":"location_select","name":"地理位置","key":"32","sub_button":[]}
		]}
	]}
}

整理后。

 

自定义菜单删除接口

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141015

请求说明

http请求方式:GET
https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN

在 /Weixin/src/com/imooc/util/WeixinUtil.java 添加一个常量

	private static final String DELETE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN";

再添加一个方法:

	public static int deleteMenu(String token){
		String url = DELETE_MENU_URL.replace("ACCESS_TOKEN", token);
		JSONObject jsonObject = doGetStr(url);
		int result = 0;
		if(jsonObject != null){
			result = jsonObject.getInt("errcode");
		}
		return result;
	}

测试

/Weixin/src/com/imooc/test/WeixinTest.java

package com.imooc.test;

import net.sf.json.JSONObject;

import com.imooc.po.AccessToken;
import com.imooc.util.WeixinUtil;

public class WeixinTest {

	public static void main(String[] args) {
		try {
			AccessToken token = WeixinUtil.getAccessToken();
			System.out.println("票据:"+token.getToken());
			System.out.println("有效时间:"+token.getExpiresIn());
		
//			String path = "G:/imooc.jpg";
//			String mediaId = WeixinUtil.upload(path, token.getToken(), "thumb");
//			System.out.println(mediaId);
			
//			String menu = JSONObject.fromObject(WeixinUtil.initMenu()).toString();
//			int result = WeixinUtil.createMenu(token.getToken(), menu);
//			if(result == 0){
//				System.out.println("菜单创建成果");
//			}else{
//				System.out.println("错误码:"+result);
//			}
			
			// 菜单查询
//			JSONObject jsonObject = WeixinUtil.queryMenu(token.getToken());
//			System.out.println(jsonObject);
			
			// 自定义菜单删除
			int result = WeixinUtil.deleteMenu(token.getToken());
			if(result==0){
				System.out.println("菜单删除成功");
			}else{
				System.out.println(result);
			}
		
		}  catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

执行输出

票据:12_YLtasb6mSLhKhs9IdNmOXINdxUROhqgPgdiUZh-eHqS8a_TSVaLwMN9EMcpTcDcbyDGvhU085SO5mhlAskbreZ_GJdbPZzrnZLE0m1NatUaiwOhdfedXlCqJ3asMZc19n0Z6wpHuZmoVOMjUQNXcAIAAXH
有效时间:7200
菜单删除成功

 

注意:菜单的创建就是菜单的修改

重新关注公众号,菜单就没有了,证明删除成功

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值