运行程序,返回errcode40016:invalid button size hint: [afL4na0498vr21]
1. 到公众号调试页面,输入token和传过去的菜单json串 https://mp.weixin.qq.com/debug/
如果创建成功,说明菜单json串 格式没有错,按钮个数也符合要求。否则请修改你的 json串。
首先保证json串的格式没有问题,看第2步:
2. 程序的问题: 应该使用 没有参数名的http的post请求。这里我使用的是httpclient工具包:
public static JSONObject sendPostWithoutParamName(String url,String paramValue) throws Exception
{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
StringEntity paramEntity = new StringEntity(paramValue,"utf-8"); //防止中文乱码
httpPost.setEntity(paramEntity); //直接放到entity中
HttpResponse response = httpClient.execute(httpPost);
return JSONObject.parseObject(EntityUtils.toString(response.getEntity()));
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
测试程序:
@Test
public void testPost() throws Exception
{
String createViewUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={红色替换成你的token}";
JSONObject tokenObj = HttpClientUtils.sendPostWithoutParamName(createViewUrl, "{\"button\":[{\"type\":\"click\",\"name\":\"今日歌曲\",\"key\":\"V1001_TODAY_MUSIC\"}]}");
Map javaObject = tokenObj.toJavaObject(Map.class);
System.out.println(javaObject.get("errcode"));
System.out.println(javaObject.get("errmsg"));
}
即可解决。