https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
package com.vx.vxsub.button;
import com.vx.vxsub.token.TokenUtil;
import com.vx.vxsub.util.HttpUtil;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class TestButton {
// main 函数生成快捷键 psvm
public static void main(String[] args) {
// 创建按钮
// 创建一级菜单
Button button=new Button();
List buttons = new ArrayList<>();
// 一级菜单中第一个按钮
ClickButton clickButton=new ClickButton("查看爆料");
clickButton.setKey("1");
// 1-2
ViewButton viewButton=new ViewButton("baidu","http://www.baidu.com");
// 1-3 二级菜单
SubButton subButton=new SubButton("更多");
List subButtons=new ArrayList<>();
// 2-1
subButtons.add(new ViewButton("千锋","http://www.qfedu.cn"));
// 2-2
subButtons.add(new PhotoOrAlbumButton("上传图片","2"));
subButton.setSub_button(subButtons);
// 把一级菜单的三个按钮添加进集和
buttons.add(clickButton);
buttons.add(viewButton);
buttons.add(subButton);
// 把集合添加到一级菜单中
button.setButton(buttons);
// 转换为json字符串 可以考虑使用fastjson
JSONObject jsonObject = JSONObject.fromObject(button);
String json = jsonObject.toString();
String url = String.format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=%s", TokenUtil.getAccessToken());
//发送请求
String result = HttpUtil.doPost(url, json);
System.out.println("result以获取:");
System.out.println(result);
}
}
这里提示报错:
result以获取:
{"errcode":40020,"errmsg":"invalid button url size rid: 66250498-6dc28843-47afdcb9"}
求解·
进程已结束,退出代码为 0
你好,日志查看请求包
{
"button": [{
"key": "1",
"name": "查看爆料",
"type": "click"
}, {
"key": "http://www.baidu.com",
"name": "baidu",
"type": "view"
}, {
"name": "更多",
"sub_button": [{
"key": "http://www.qfedu.cn",
"name": "千锋",
"type": "view"
}, {
"key": "2",
"name": "上传图片",
"type": "pic_photo_or_album"
}]
}]
}
你可以打印一下 json,看下是否符合文档中的要求