java微信开发自定义菜单
2015-08-11 19:53
751 查看
/** * */ package com.caiyl.zmd.weixin; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; 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.HttpClientBuilder; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; public class WeixinMenuUtils { // http客户端 private static HttpClient httpclient; static { httpclient = HttpClientBuilder.create().build(); } public static void main(String[] args) { try { // 获取accessToken -参数appid,secret String accessToken = getAccessToken("appid", "secret"); System.out.println(accessToken); // 创建菜单 // String s = "{\"button\":[{\"name\":\"休闲娱乐\",\"sub_button\":[{\"type\":\"click\",\"name\":\"笑话大全\",\"key\":\"m_joke\"},{\"type\":\"click\",\"name\":\"内涵段子\",\"key\":\"m_duanzi\"},{\"type\":\"click\",\"name\":\"爆笑图片\",\"key\":\"m_laughImg\"}]},{\"name\":\"实用工具\",\"sub_button\":[{\"type\":\"click\",\"name\":\"天气查询\",\"key\":\"m_weather\"},{\"type\":\"click\",\"name\":\"公交查询\",\"key\":\"m_bus\"},{\"type\":\"click\",\"name\":\"功能菜单\",\"key\":\"m_sysmenu\"}]},{\"name\":\"消息示例\",\"sub_button\":[{\"type\":\"click\",\"name\":\"关于企特\",\"key\":\"m_about\"},{\"type\":\"click\",\"name\":\"图文消息\",\"key\":\"m_imgmsg\"},{\"type\":\"click\",\"name\":\"音乐消息\",\"key\":\"m_musicmsg\"}]}]}"; // String res = createMenu(s, accessToken); // System.out.println(res); // 查询菜单 String info = getMenuInfo(accessToken); System.out.println(info); } catch (Exception e) { e.printStackTrace(); } } /** * 获取accessToken */ public static String getAccessToken(String appid, String secret) throws Exception { HttpGet get=new HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret+""); HttpResponse response = httpclient.execute(get); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject object = JSON.parseObject(jsonStr); return object.getString("access_token"); } /** * 创建菜单 */ public static String createMenu(String params, String accessToken) throws Exception { HttpPost httpost = new HttpPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + accessToken); httpost.setEntity(new StringEntity(params, "UTF-8")); HttpResponse response = httpclient.execute(httpost); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(jsonStr); JSONObject object = JSON.parseObject(jsonStr); return object.getString("errmsg"); } /** * 查询菜单 */ public static String getMenuInfo(String accessToken) throws Exception { HttpGet get = new HttpGet("https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + accessToken); HttpResponse response = httpclient.execute(get); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); return jsonStr; } /** * 删除自定义菜单 */ public static String deleteMenu(String accessToken) throws Exception { HttpGet get = new HttpGet("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=" + accessToken); HttpResponse response = httpclient.execute(get); String jsonStr = EntityUtils.toString(response.getEntity(), "utf-8"); JSONObject object = JSON.parseObject(jsonStr); return object.getString("errmsg"); } }
相关文章推荐
- 微信公共服务平台开发(.Net 的实现)10-------地理位置
- 微信公众平台开发 用户分组接口
- android微信支付(问题总结)
- Android 微信授权登陆
- DIY一个万能小程序
- 转: 从微信的故障谈谈服务可用性
- 如何获取微信文章阅读数和点赞数
- 我的微信
- 微信开发处理超时5s(java),异步发送客服消息
- 一处折腾笔记:Android内嵌html5添加原生微信分享的解决办法
- iOS 微信支付的那些事
- 简单实现微信录音+转码(.MP3)
- 【面经】人人,金山西山居,腾讯互娱,微信,网易游戏offer及面经
- 浅谈微信三级分销系统的漏洞
- 我的微信订阅号开发--新手篇
- 浅谈微信三级分销系统的漏洞
- 微信SDK开发学习第二课
- C#微信结合百度api获取当前用户的地理位置
- 微信商城无法使用支付宝支付已经成为过去式
- 微信倡导全民“无现金日”,最高送88元!