菜单创建
接口说明
通过POST一个特定结构体,实现在微信客户端创建自定义菜单。
请求说明
http请求方式:POST
https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
结构体其实就是一个json格式的字符串,相信很多朋友就卡在这里了,怎么往这个接口地址 post 一个结构体呢,实现代码如下:
public static void main(String[] args) throws Exception {
URL url = new URL("https://api.weixin.qq.com/cgi-bin /menu/create?access_token=TOKEN");
String responeJsonStr = "{"+
"\"button\":["+
"{\"name\":\"酒店预订\","+
"\"sub_button\":["+
"{"+
"\"type\":\"click\","+
"\"name\":\"附近酒店\","+
"\"key\":\"jintoneinn488878-1\""+
"},"+
"{"+
"\"type\":\"click\","+
"\"name\":\"查找酒店\","+
"\"key\":\"jintoneinn488878-2\""+
"}"+
"]"+
"}"
"]"+
"}";
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.connect();
conn.getOutputStream().write(responeJsonStr.getBytes("utf-8"));
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is,"utf-8"));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
}
System.out.println(buffer.toString());
}