package com.gist.demo_offline;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HttpsURLConnection;
import org.junit.Test;
import com.gist.bean.FatherButton;
import com.gist.bean.MatchRule;
import com.gist.bean.Menu;
import com.gist.bean.SonButton;
import com.gist.utils.AccessTokenUtils;
import com.google.gson.Gson;
/**
* @author 高远</n> 邮箱:wgyscsf@163.com</n> 博客 http://blog.csdn.net/wgyscsf</n>
* 编写时期 2016-4-6 下午8:44:43
*/
/*
* 该类测试了直接获取Access_token和保存后判断Access_token存活时间后再获取两种方式【使用时,使用后者】。
*/
/*
* 使用:createCommMenu()//常见自定义菜单;createSpecialMenuJson()//创建个性化菜单
*/
public class MyMenuDemo {
/*
* 创建自定义菜单。
*/
@Test
public void createCommMenu() {
String ACCESS_TOKEN = AccessTokenUtils.getAccessToken();// 获取AccessToken,AccessTokenUtils是封装好的类
// 拼接api要求的httpsurl链接
String urlString = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="
+ ACCESS_TOKEN;
try {
// 创建一个url
URL reqURL = new URL(urlString);
// 拿取链接
HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
.openConnection();
httpsConn.setDoOutput(true);
// 取得该连接的输出流,以读取响应内容
OutputStreamWriter osr = new OutputStreamWriter(
httpsConn.getOutputStream());
osr.write(getMenuJson());// 使用本类外部方法getMenuJson()
osr.close();
// 返回结果
InputStreamReader isr = new InputStreamReader(
httpsConn.getInputStream());
// 读取服务器的响应内容并显示
char[] chars = new char[1024];
String reslut = "";
int len;
while ((len = isr.read(chars)) != -1) {
reslut += new String(chars, 0, len);
}
System.out.println("返回结果:" + reslut);
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getMenuJson() {
/*
* 任务(注释:“m-0”表示父按钮;“m-n”表示第m个父按钮,第n个子按钮(m,n≠0)):1-0:名字:click,响应点击事件:点击推事件
* 。2-0:名
* 字:父按钮2。2-1:名字:view,响应事件:跳转网页;2-2:名字:scancode_push,响应事件:扫码推事件;2-
* 3:名字:scancode_waitmsg
* ,响应事件:扫码推事件且弹出“消息接收中”提示框;2-4:名字:pic_sysphoto,响应事件
* :弹出系统拍照发图。2-5:名字:pic_photo_or_album,响应事件:弹出拍照或者相册发图。3-0:名
* 字:父按钮3。3-1:名字
* :pic_weixin,响应事件:弹出微信相册发图器;3-2:名字:location_select,响应事件:弹出地理位置选择器
* ;3-3:名字:media_id
* ,响应事件:下发消息(除文本消息);3-4:名字:view_limited,响应事件:跳转图文消息url。
*/
Gson gson = new Gson();// json处理工具
Menu menu = new Menu();// 菜单类
List<FatherButton> fatherButtons = new ArrayList<FatherButton>();// 菜单中的父按钮集合
// -----------
// 父按钮1
FatherButton fb1 = new FatherButton();
fb1.setName("click");
fb1.setType("click");
fb1.setKey("10");
// -------------
// 父按钮2
FatherButton fb2 = new FatherButton();
fb2.setName("父按钮2");
List<SonButton> sonButtons2 = new ArrayList<SonButton>();// 子按钮的集合
// 子按钮2-1
SonButton sb21 = new SonButton();
sb21.setName("view");
sb21.setUrl("http://www.baidu.com");
sb21.setType("view");
// 子按钮2-2
SonButton sb22 = new SonButton();
sb22.setName("scancode_push");
sb22.setType("scancode_push");
sb22.setKey("22");
// 子按钮2-3
SonButton sb23 = new SonButton();
sb23.setName("scancode_waitmsg");
sb23.setType("scancode_waitmsg");
sb23.setKey("23");
// 子按钮2-4
SonButton sb24 = new SonButton();
sb24.setName("pic_sysphoto");
sb24.setType("pic_sysphoto");
sb24.setKey("24");
// 子按钮2-5
SonButton sb25 = new SonButton();
sb25.setName("pic_photo_or_album");
sb25.setType("pic_photo_or_album");
sb25.setKey("25");
// 添加子按钮到子按钮集合
sonButtons2.add(sb21);
sonButtons2.add(sb22);
sonButtons2.add(sb23);
sonButtons2.add(sb24);
sonButtons2.add(sb25);
// 将子按钮放到2-0父按钮集合
fb2.setSonButtons(sonButtons2);
// ------------------
// 父按钮3
FatherButton fb3 = new FatherButton();
fb3.setName("父按钮3");
List<SonButton> sonButtons3 = new ArrayList<SonButton>();
// 子按钮3-1
SonButton sb31 = new SonButton();
sb31.setName("pic_weixin");
sb31.setType("pic_weixin");
sb31.setKey("31");
// 子按钮3-2
SonButton sb32 = new SonButton();
sb32.setName("locatselect");
sb32.setType("location_select");
sb32.setKey("32");
// // 子按钮3-3-->测试不了,因为要media_id。这需要调用素材id.
// SonButton sb33 = new SonButton();
// sb33.setName("media_id");
// sb33.setType("media_id");
// sb33.setMedia_id("???");
// // 子按钮3-4-->测试不了,因为要media_id。这需要调用素材id.
// SonButton sb34 = new SonButton();
// sb34.setName("view_limited");
// sb34.setType("view_limited");
// sb34.setMedia_id("???");
// 添加子按钮到子按钮队列
sonButtons3.add(sb31);
sonButtons3.add(sb32);
// sonButtons3.add(sb33);
// sonButtons3.add(sb34);
// 将子按钮放到3-0父按钮队列
fb3.setSonButtons(sonButtons3);
// ---------------------
// 将父按钮加入到父按钮集合
fatherButtons.add(fb1);
fatherButtons.add(fb2);
fatherButtons.add(fb3);
// 将父按钮队列加入到菜单栏
menu.setFatherButtons(fatherButtons);
String json = gson.toJson(menu);
System.out.println(json);// 测试输出
return json;
}
@Test
public void createSpecialMenuJson() {
String ACCESS_TOKEN = AccessTokenUtils.getAccessToken();// 获取AccessToken
// 拼接api要求的httpsurl链接
String urlString = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token="
+ ACCESS_TOKEN;
try {
// 创建一个url
URL reqURL = new URL(urlString);
// 拿取链接
HttpsURLConnection httpsConn = (HttpsURLConnection) reqURL
.openConnection();
httpsConn.setDoOutput(true);
// 取得该连接的输出流,以读取响应内容
OutputStreamWriter osr = new OutputStreamWriter(
httpsConn.getOutputStream());
osr.write(getSpecialMenuJson());
osr.close();
// 返回结果
InputStreamReader isr = new InputStreamReader(
httpsConn.getInputStream());
// 读取服务器的响应内容并显示
char[] chars = new char[1024];
String reslut = "";
int len;
while ((len = isr.read(chars)) != -1) {
reslut += new String(chars, 0, len);
}
System.out.println("返回结果:" + reslut);
isr.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getSpecialMenuJson() {
/*
* 任务(注释:“m-0”表示父按钮;“m-n”表示第m个父按钮,第n个子按钮(m,n≠0)):1-0:名字:click,响应点击事件:点击推事件
* 。2-0:名
* 字:父按钮2。2-1:名字:view,响应事件:跳转网页;2-2:名字:scancode_push,响应事件:扫码推事件;2-
* 3:名字:scancode_waitmsg
* ,响应事件:扫码推事件且弹出“消息接收�
没有合适的资源?快使用搜索试试~ 我知道了~
java微信开发API解析(四)-自定义菜单以及个性化菜单实现
共45个文件
java:13个
class:13个
xml:3个
4星 · 超过85%的资源 需积分: 18 377 下载量 55 浏览量
2016-04-09
14:05:07
上传
评论 5
收藏 864KB ZIP 举报
温馨提示
文章同步:http://blog.csdn.net/wgyscsf/article/details/51104855
资源推荐
资源详情
资源评论
收起资源包目录
java微信开发API解析(四)-自定义菜单以及个性化菜单实现.zip (45个子文件)
WeixinApiDemo
xml.xml 272B
src
com
gist
demo_offline
Access_tokenDemo.java 6KB
MyMenuDemo.java 11KB
utils
AccessTokenUtils.java 5KB
bean
Access_token.java 756B
Article.java 1010B
SonButton.java 405B
PicAndTextMsg.java 2KB
Menu.java 725B
BaseButton.java 905B
MatchRule.java 1KB
TextMsg.java 2KB
FatherButton.java 700B
servlet
CoreServlet.java 10KB
README 302B
.classpath 828B
.mymetadata 312B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 477B
.jsdtscope 500B
org.eclipse.jdt.core.prefs 364B
temp_access_token.temp 186B
.project 2KB
WebRoot
META-INF
MANIFEST.MF 36B
index.jsp 829B
WEB-INF
classes
com
gist
demo_offline
Access_tokenDemo.class 5KB
MyMenuDemo.class 6KB
utils
AccessTokenUtils.class 4KB
bean
MatchRule.class 2KB
PicAndTextMsg.class 2KB
TextMsg.class 2KB
Article.class 1KB
FatherButton.class 1KB
SonButton.class 556B
Access_token.class 1KB
BaseButton.class 1KB
Menu.class 1KB
servlet
CoreServlet.class 7KB
README 302B
lib
dom4j-1.6.1.jar 307KB
gson-2.3.1.jar 206KB
xstream-1.3.jar 401KB
web.xml 404B
共 45 条
- 1
资源评论
- mike07232017-12-01感谢分享。。
- lhqbighead20052019-01-10可以用。不错
- daichenghua2018-07-31....... ok!
_高远
- 粉丝: 476
- 资源: 27
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功