使用Java进行微信开发 4 创建自定义菜单
2018-02-28 13:58
676 查看
1 方式一:不写代码,使用微信公众平台接口调试工具来创建菜单
2 方式二:写代码创建菜单
ViewButton
ClickButton
package com.iris.wechat.menu;
public class ClickButton {
private String type;
private String name;
private String key;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
CreateMenu
{ "button": [ { "name": "百度", "type": "view", "url": "http://www.baidu.com" }, { "name": "菜单", "sub_button": [ { "key": "text", "name": "回复图文", "type": "click" }, { "name": "百度", "type": "view", "url": "http://www.baidu.com" } ] }, { "key": "text", "name": "回复图文", "type": "click" } ] } |
ViewButton
package com.iris.wechat.menu; /** * 视图型菜单事件 * @author ifeng * */ public class ViewButton { private String type; private String name; private String url; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } }
ClickButton
package com.iris.wechat.menu;
public class ClickButton {
private String type;
private String name;
private String key;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
}
CreateMenu
package com.iris.wechat.menu; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class CreateMenu { public static void main(String[] args){ String accessToken = null; try { accessToken = AccessToken.getAccessToken(); } catch (Exception e) { System.out.println("fail to get AccessToken"); } if (accessToken !=null ){ ClickButton cbt=new ClickButton(); cbt.setKey("image"); cbt.setName("回复图片1"); cbt.setType("click"); ViewButton vbt=new ViewButton(); vbt.setUrl("http://irisfeng.natapp1.cc/WechatValidation/toSAP.action"); vbt.setName("ToSAP"); vbt.setType("view"); JSONArray sub_button=new JSONArray(); sub_button.put(cbt); sub_button.put(vbt); JSONObject buttonOne=new JSONObject(); buttonOne.put("name", "菜单"); buttonOne.put("sub_button", sub_button); JSONArray button=new JSONArray(); button.put(vbt); button.put(buttonOne); button.put(cbt); JSONObject menujson=new JSONObject(); menujson.put("button", button); System.out.println(menujson); String postURL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessToken; try { URL url = new URL(postURL); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); OutputStream outputStream = connection.getOutputStream(); outputStream.write(menujson.toString().getBytes("UTF-8")); outputStream.flush(); outputStream.close(); InputStream inputStream = connection.getInputStream(); int size =inputStream.available(); byte[] bs =new byte[size]; inputStream.read(bs); String message=new String(bs,"UTF-8"); System.out.println("message="+message); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } // public static String getMenuStr() throws JSONException{ // JSONObject firstLevelMenu = JSONObject.fromObject("");//一级菜单 // JSONArray firstLevelMenuArray = new JSONArray();//一级菜单列表 // // // //一级菜单内容1 // JSONObject firstLevelMenuContext1 = new JSONObject(); // firstLevelMenuContext1.put("type", "click"); // firstLevelMenuContext1.put("name", "歌曲"); // firstLevelMenuContext1.put("key", "V1001_TODAY_MUSIC"); // // //一级菜单内容2 // JSONObject firstLevelMenuContext2 = new JSONObject(); // //一级菜单内容2的二级菜单列表 // JSONArray firstLevelMenuContext2Array = new JSONArray(); // //一级菜单内容2的二级菜单内容1 // JSONObject jsonObject1 = new JSONObject(); // jsonObject1.put("type", "click"); // jsonObject1.put("name", "歌曲"); // jsonObject1.put("key", "V1001_TODAY_MUSIC"); // //一级菜单内容2的二级菜单内容2 // JSONObject jsonObject2 = new JSONObject(); // jsonObject2.put("type", "view"); // jsonObject2.put("name", "视频"); // jsonObject2.put("url", "http://www.randzh.cn"); // firstLevelMenuContext2Array.put(jsonObject1); // firstLevelMenuContext2Array.put(jsonObject2); // firstLevelMenuContext2.put("name", "菜单"); // firstLevelMenuContext2.put("sub_button", firstLevelMenuContext2Array); // // //一级菜单内容3 // JSONObject firstLevelMenuContext3 = new JSONObject(); // firstLevelMenuContext3.put("type", "click"); // firstLevelMenuContext3.put("name", "视频"); // firstLevelMenuContext3.put("key", "V1001_TODAY_MOVIE"); // // // firstLevelMenuArray.put(firstLevelMenuContext1); // firstLevelMenuArray.put(firstLevelMenuContext2); // firstLevelMenuArray.put(firstLevelMenuContext1); // // // firstLevelMenu.put("button", firstLevelMenuArray); // // String str = firstLevelMenu.toString(); // System.out.println("JSON str="+str); // return str; // } // // public static void createCustomMenu() throws Exception{ // String custmMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="; // // //获取access_token // String accessToken = AccessToken.getAccessToken(); // custmMenuUrl = custmMenuUrl + accessToken; // System.out.println("===custmMenuUrl="+custmMenuUrl); // // URL url = new URL(custmMenuUrl); // HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // // connection.setRequestMethod("POST"); // connection.setDoOutput(true); // connection.setDoInput(true); // connection.connect(); // // OutputStream outputStream = connection.getOutputStream(); // outputStream.write(getMenuStr().getBytes("UTF-8")); // outputStream.flush(); // outputStream.close(); // // InputStream inputStream = connection.getInputStream(); // int size =inputStream.available(); // byte[] bs =new byte[size]; // inputStream.read(bs); // String message=new String(bs,"UTF-8"); // // System.out.println("message="+message); //} }
相关文章推荐
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应 (Java版)
- 微信公众帐号自定义菜单创建及事件响应开发教程 附java源代码
- Java微信公众平台开发(十)——微信自定义菜单的创建实现
- Java微信公众平台开发(十)——微信自定义菜单的创建实现
- 夺命雷公狗---微信开发15----编写进行创建自定义菜单
- Java微信公众平台开发(10) 微信自定义菜单的创建实现
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- 微信公众平台开发教程Java版(五) 自定义菜单创建
- 微信开发第一篇,使用Java创建成为开发者的应用
- [038] 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- [038] 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- 二十四:微信公众帐号开发-自定义菜单的创建及菜单事件响应
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应
- [038] 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应