1.首先分析创建菜单的数据结构
创建三个pojo对象,他们之间的关系如图
pojo的数据转化为json数据格式
{
"button":[
{"name":"林青霞",
"sub_button":
[
{"name":"天龙八部1-天山童姥","sub_button":[],"type":"view","url":"https://www.zhihu.com/question/34508635"},
{"name":"笑傲江湖2-东方不败","sub_button":[],"type":"view","url":"https://baike.baidu.com/item/%E5%A4%A9%E9%BE%99%E5%85%AB%E9%83%A8%E4%B9%8B%E5%A4%A9%E5%B1%B1%E7%AB%A5%E5%A7%A5/9325498?fr=aladdin&fromid=2843437&fromtitle=%E6%96%B0%E5%A4%A9%E9%BE%99%E5%85%AB%E9%83%A8%E4%B9%8B%E5%A4%A9%E5%B1%B1%E7%AB%A5%E5%A7%A5"},
{"name":"笑傲江湖3-风云再起","sub_button":[],"type":"view","url":"https://baike.baidu.com/item/%E4%B8%9C%E6%96%B9%E4%B8%8D%E8%B4%A5%E9%A3%8E%E4%BA%91%E5%86%8D%E8%B5%B7/3631758?fr=aladdin"}
],
"type":""
},
{"name":"我是谁?",
"sub_button":
[
{"name":"倚天一出","sub_button":[],"type":"view","url":"http://blog.csdn.net/Edison_03/article/category/6991773"},
{"name":"谁与争锋","sub_button":[],"type":"view","url":"https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013"}
],
"type":""
},
{"name":"微信测试",
"sub_button":
[
{"key":"21","name":"点击扫码","sub_button":[],"type":"scancode_push"},
{"key":"31","name":"位置选择","sub_button":[],"type":"location_select"}],
"type":""}
]
}
2.获取access_token
- getAccessToken方法返回AccessToken 对象,点我查看如何获取access_token
/**
* 必须先后获取accessToken,才能调用相应的高级接口
* @return
*/
public static AccessToken getAccessToken(){
AccessToken token = new AccessToken();
String url = AccessTokenUrl.replace("APPID", APPID).replaceAll("APPSECRET", APPSECRET);//替换到接口地址中的APPID和APPSECRET
JSONObject jsonObject = doGet(url);//调用doGet方法发送get请求到url
System.out.println("微信服务器返回的accessToken数据:"+jsonObject);
if (jsonObject!=null) {
token.setAccess_token(jsonObject.getString("access_token"));
token.setExpires_in(jsonObject.getInt("expires_in"));
}
return token;
}
3.创建工具类,目的发送符合要求的json数据
- initMenu方法,初始化具有特定功能的菜单
public static Menu initMenu(){
ViewButton secondButton11 = new ViewButton();
secondButton11.setName("笑傲江湖2-东方不败");
secondButton11.setType(messageUtil.userResp_message_type_VIEW);
secondButton11.setUrl("https://www.zhihu.com/question/34508635");
ViewButton secondButton12 = new ViewButton();
secondButton12.setName("天龙八部1-天山童姥");
secondButton12.setType(messageUtil.userResp_message_type_VIEW);
secondButton12.setUrl("https://baike.baidu.com/item/%E5%A4%A9%E9%BE%99%E5%85%AB%E9%83%A8%E4%B9%8B%E5%A4%A9%E5%B1%B1%E7%AB%A5%E5%A7%A5/9325498?fr=aladdin&fromid=2843437&fromtitle=%E6%96%B0%E5%A4%A9%E9%BE%99%E5%85%AB%E9%83%A8%E4%B9%8B%E5%A4%A9%E5%B1%B1%E7%AB%A5%E5%A7%A5");
ViewButton secondButton13 = new ViewButton();
secondButton13.setName("笑傲江湖3:风云再起");
secondButton13.setType(messageUtil.userResp_message_type_VIEW);
secondButton13.setUrl("https://baike.baidu.com/item/%E4%B8%9C%E6%96%B9%E4%B8%8D%E8%B4%A5%E9%A3%8E%E4%BA%91%E5%86%8D%E8%B5%B7/3631758?fr=aladdin");
Button button1 = new Button();//第一个一级菜单,下有3个view类型的子菜单
button1.setName("林青霞");
button1.setSub_button(new Button[]{secondButton12,secondButton11,secondButton13});
ViewButton secondButton21 = new ViewButton();
secondButton21.setName("倚天一出");
secondButton21.setType(messageUtil.userResp_message_type_VIEW);
secondButton21.setUrl("http://blog.csdn.net/Edison_03/article/category/6991773");
ViewButton secondButton22 = new ViewButton();
secondButton22.setName("谁与争锋");
secondButton22.setType(messageUtil.userResp_message_type_VIEW);
secondButton22.setUrl("https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141013");
Button button2 = new Button();//第二个一级菜单,下有3个view类型的子菜单
button2.setName("我是谁?");
button2.setSub_button(new Button[]{secondButton21,secondButton22});
ClickButton firstButton21 = new ClickButton();
firstButton21.setType("scancode_push");
firstButton21.setName("点击扫码");
firstButton21.setKey("21");
ClickButton firstButton31 = new ClickButton();
firstButton31.setType("location_select");
firstButton31.setName("位置选择");
firstButton31.setKey("31");
Button button3 = new Button();//第二个一级菜单,下有3个view类型的子菜单
button3.setName("微信测试");
button3.setSub_button(new Button[]{firstButton21,firstButton31});
Menu menu = new Menu();
menu.setButton(new Button[]{button1,button2,button3});
return menu;
}
- createMenu方法,将json数据发送到微信指定url,创建菜单
public static int createMenu(String token,String menu){
int errcode = 0;
String url = CreatMenuUrl.replace("ACCESS_TOKEN", token);
JSONObject jsonObject = doPost(url,menu);
if (jsonObject!=null) {
errcode=jsonObject.getInt("errcode");
}
return errcode;
}
- doPost方法,发送协议为HTTPS的post请求
public static JSONObject doPost(String url,String outUrl){
JSONObject jsonObject=null;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost();
httpPost.setEntity(new StringEntity(outUrl,"UTF-8"));
URI uri = null;
try {
uri = new URI(url);//视频中忘了加url,所以输出httpPost,会显示POST null HTTP/1.1
}
catch (URISyntaxException e1) {
e1.printStackTrace();
}
httpPost.setURI(uri);
System.out.println(httpPost);//POST null HTTP/1.1
try {
HttpResponse reponse = httpClient.execute(httpPost);
String result = EntityUtils.toString(reponse.getEntity(),"UTF-8");
jsonObject=JSONObject.fromObject(result);
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
- 运行代码,创建菜单即可
@Test
public void testCreateMenu() throws Exception{
Menu initMenu = weiXinUtil.initMenu();
String menu = JSONObject.fromObject(initMenu).toString();
int result = weiXinUtil.createMenu(weiXinUtil.getAccessToken().getAccess_token(),menu);
if (result==0) {
System.out.println("创建菜单成功");
}else {
System.out.println(result);
}
}
4.测试创建菜单效果
- 点击“林青霞”
- 点击“我是谁?”
- 点击“微信测试”