您的位置:首页 > 移动开发 > 微信开发

java版微信公众平台自定义菜单创建代码实现

2013-08-09 18:11 441 查看

微信公众平台自定义菜单创建代码实现—java版

搞了两天的自定义菜单,终于搞定了,现在分享下心得,以便后来者少走弯路......

好了,先看先微信官方的API

index.jsp
<%@page import="java.io.*"%>
<%@page import="java.net.*" %>
<%@page import="org.json.*" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>

<%
final String appId = " ";
final String appSecret = " "; //自己的APPIP 和APPSECRET

%>
<%
class TestGetPost{

public String getAccess_token(){ // 获得ACCESS_TOKEN

String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appId + "&secret=" +appSecret;

String accessToken = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();

http.setRequestMethod("GET"); //必须是get方式请求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒

http.connect();

InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");

JSONObject demoJson = new JSONObject(message);
accessToken = demoJson.getString("access_token");

System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
public int createMenu() throws IOException {
String user_define_menu = "{\"button\":[{\"type\":\"click\",\"name\":\"项目管理\",\"key\":\"20_PROMANAGE\"},{\"type\":\"click\",\"name\":\"机构运作\",\"key\":\"30_ORGANIZATION\"},{\"name\":\"日常工作\",\"sub_button\":[{\"type\":\"click\",\"name\":\"待办工单\",\"key\":\"01_WAITING\"},{\"type\":\"click\",\"name\":\"已办工单\",\"key\":\"02_FINISH\"},{\"type\":\"click\",\"name\":\"我的工单\",\"key\":\"03_MYJOB\"},{\"type\":\"click\",\"name\":\"公告消息箱\",\"key\":\"04_MESSAGEBOX\"},{\"type\":\"click\",\"name\":\"签到\",\"key\":\"05_SIGN\"}]}]}";
//此处改为自己想要的结构体,替换即可
String access_token= getAccess_token();

String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();

http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //读取超时30秒

http.connect();
OutputStream os= http.getOutputStream();
os.write(user_define_menu.getBytes("UTF-8"));//传入参数
os.flush();
os.close();

InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
System.out.println(message);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 0;
}
}%>
<%
TestGetPost tgp = new TestGetPost();

tgp.createMenu();
%>

index.jsp

部署好后,直接在浏览器地址栏中输入自己百度云开发者中心下改应用的当前域名即可,然后关注微信公众账号看效果(已关注的,取消关注并重新关注,即可看到效果)。

记得将应用中的xml文件里的

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

创建后效果为:



小提示:

1、自定义菜单的查看:直接在浏览器地址栏中输入

https://api.weixin.qq.com/cgi-bin/menu/get?access_token=ACCESS_TOKEN  (换成自己的access_token,access_token在一段时间里是有效的,所以获得后,在粘贴到此是没有问题的)

2、自定义菜单的删除:

https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN (删除与查看同理)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: