java微信自定义菜单(java微信开发学习笔记5)(完)(整个项目的源代码在最后)
来源:互联网 发布:恢复删除软件要钱 编辑:程序博客网 时间:2024/06/05 16:56
我的微信公众号是个人订阅号,个人订阅号不能认证,所以没有自定义菜单的权限。我使用的是微信公众平台提供的一个测试账号。
测试号里面会提供一个appid和appsecret,同样需要接口配置。
创建菜单:
package com.weixin.util;import java.io.IOException;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.ParseException;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import com.alibaba.fastjson.JSON;import com.weixin.menu.Button;import com.weixin.menu.ClickButton;import com.weixin.menu.Menu;import com.weixin.menu.ViewButton;import net.sf.json.JSONObject;public class HttpUtil { private static final String APPID = "APPID"; private static final String APPSECRET = "APPSECRET"; private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET"; private static final String CREATE_MENU_URL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN"; /** * get请求 * @param url * @return */ public static JSONObject doGetStr(String url) throws ParseException, IOException{ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); JSONObject jsonObject = null; HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { String result = EntityUtils.toString(entity, "UTF-8"); jsonObject = JSONObject.fromObject(result); } return jsonObject; } /** * post请求 * @param url * @param outStr * @return */ public static JSONObject doPostStr(String url,String outStr){ DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); JSONObject jsonObject = null; httpPost.setEntity(new StringEntity(outStr, "UTF-8")); try { HttpResponse response = httpClient.execute(httpPost); String result = EntityUtils.toString(response.getEntity(), "UTF-8"); jsonObject = JSONObject.fromObject(result); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObject; } /** * 通过判断获取AccessToken:本地-网络 * @return * @throws IOException */ public static String getAccessToken() throws IOException{ String filePath = "E:/zx/access_token/access_token01.txt"; String access_token = null; String read= ReadAndWriteTxt.readFile(filePath); if (read.equals("001")) { access_token = getAccessTokenHttp(); String str = access_token+"++"+ReadAndWriteTxt.getTime; ReadAndWriteTxt.writeFile(filePath, str); System.out.println("new"); }else if((ReadAndWriteTxt.getTime - Integer.valueOf(read.split("++")[1]).intValue()) > 7200){ access_token = getAccessTokenHttp(); String str = access_token+"++"+ReadAndWriteTxt.getTime; ReadAndWriteTxt.writeFile(filePath, str); System.out.println("update"); }else { access_token = read.split("++")[0]; System.out.println("show"); } return access_token; } /** * 获取微信的AccessToken * @return * @throws ParseException * @throws IOException */ @SuppressWarnings("unchecked") public static String getAccessTokenHttp() throws ParseException, IOException{ String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET); JSONObject jsonObject = doGetStr(url); Map<String, String> maps = null; if (jsonObject != null) { maps = (Map<String, String>) JSON.parse(jsonObject.toString()); } String access_token = maps.get("access_token"); return access_token; } /** * 初始化菜单 * @return */ public static Menu initMenu(){ Menu menu = new Menu(); ClickButton button11 = new ClickButton(); button11.setName("click菜单"); button11.setType("click"); button11.setKey("11"); ViewButton button21 = new ViewButton(); button21.setName("view菜单"); button21.setType("view"); button21.setUrl("https://www.baidu.com/"); ClickButton button31 = new ClickButton(); button31.setName("扫码事件"); button31.setType("scancode_push"); button31.setKey("31"); ClickButton button32 = new ClickButton(); button32.setName("地理位置"); button32.setType("location_select"); button32.setKey("32"); Button button = new Button(); button.setName("菜单"); button.setSub_button(new Button[]{button31,button32}); menu.setButton(new Button[]{button11,button21,button}); return menu; } public static int creatMenu(String token,String menu){ int result = 0; String url = CREATE_MENU_URL.replace("ACCESS_TOKEN", token); JSONObject jsonObject = doPostStr(url, menu); if (jsonObject != null) { result = jsonObject.getInt("errcode"); } return result; }}
写一个测试类
package com.weixin.text;import java.io.IOException;import com.weixin.util.HttpUtil;import net.sf.json.JSONObject;public class Test { public static void main(String[] args) throws IOException{ String menu = JSONObject.fromObject(HttpUtil.initMenu()).toString(); int result = HttpUtil.creatMenu(HttpUtil.getAccessToken(), menu); if (result == 0) { System.out.println("创建菜单成功"); }else { System.out.println("错误码:"+result); } }}
源码:
http://download.csdn.net/download/zxxz5201314/9933934
PS:终于审核过了。。。。
阅读全文
0 0
- java微信自定义菜单(java微信开发学习笔记5)(完)(整个项目的源代码在最后)
- 微信公众号开发---自定义菜单的创建及菜单事件响应(java)
- java微信开发自定义菜单
- 微信自定义菜单的创建(微信账号绑定功能的实现 Java)
- 微信开发(二)自定义菜单
- 微信开发(1)自定义菜单
- java微信开发API解析(四)-自定义菜单以及个性化菜单实现
- java微信自动回复机器人开发(java微信开发学习笔记3)
- Java之微信开发自定义菜单的实现
- JAVA微信自定义菜单
- 微信公众帐号开发教程第14篇-自定义菜单的创建及菜单事件响应 (Java版)
- Java微信开发二(通过java程序自定义公众号菜单)
- 微信自定义菜单 ViewButton绑定openid(Java)
- JAVA微信订阅号开发者模式接入(java微信开发学习笔记1)
- java微信接收并回复文本信息(java微信开发学习笔记2)
- java微信获取access_token(java微信开发学习笔记4)
- 微信公众帐号自定义菜单创建及事件响应开发教程 附java源代码
- Java微信公众平台开发(十)——微信自定义菜单的创建实现
- 机器学习第五周(二)
- ubuntu下安装anaconda
- 2017学习计划
- aliyunyuan rpmforge NTFS-3G
- Unity5.X3D 游戏开发技术详解与典型案例-笔记
- java微信自定义菜单(java微信开发学习笔记5)(完)(整个项目的源代码在最后)
- java(安全方便的从控制台读入数据)[对Scanner类进行封装,用正则表达式判断]
- HDOJ1285 确定比赛名次(拓扑排序)
- Timer的使用 倒计时
- 提升编程能力的11个技巧
- [python3.6 flask web学习]Flask用户认证框架
- font-size:0的作用
- 二分查找算法
- wcf/webservice