微信公众平台开发[6]-开发模式-高级功能-自定义菜单的管理

我在这里做一个这样的假设,假设看到此文章的读者如果有用户已经通过申请获取自定义菜单的权限。


1、每次调用都会覆盖之前的自定义菜单;

2、自定义菜单更新后,并不是实时生效,有一个缓存的过程,最长是一天;

3、每个菜单名称字数为6个;

4、一级菜单最多为3个;

5、二级菜单最多为5个;

6、紧支持点击后触发消息。

有些读者可能会问,看到过一些公众号的自定义菜单,居然点击之后直接打开网页,这种方式之前微信是支持的,后来取消了,我猜测微信方不想让自己只作为一个3G网站的跳板而取消了此项功能。


创建\更新自定义菜单程序如下:(因是覆盖,故只有一个接口地址)

package weixin;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;

import weixin.MySecureProtocolSocketFactory;

public class ClickMenuCreate {

	public static void main(String[] args) {
		HttpClient httpclient;
		String access_token;
		String url;
		String menu;
		ProtocolSocketFactory fcty;
		StringRequestEntity requestEntity = null;
		PostMethod method;
		int statusCode;

		access_token = "${access_token}";
		menu = "{\"button\":[{\"type\":\"click\",\"name\":\"今日歌曲\",\"key\":\"V1001_TODAY_MUSIC\"},{\"type\":\"click\",\"name\":\"歌手简介\",\"key\":\"V1001_TODAY_SINGER\"},{\"name\":\"菜单\",\"sub_button\":[{\"type\":\"click\",\"name\":\"hello word\",\"key\":\"V1001_HELLO_WORLD\"},{\"type\":\"click\",\"name\":\"赞一下我们\",\"key\":\"V1001_GOOD\"}]}]}";
		url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + access_token;

		httpclient = new HttpClient();

		//https 证书
		fcty = new MySecureProtocolSocketFactory();
		Protocol.registerProtocol("https", new Protocol("https", fcty, 443));

		//post

		try {
			requestEntity = new StringRequestEntity(menu, "application/json", "UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		method = new PostMethod(url);
		method.setRequestEntity(requestEntity);

		try {
			statusCode = httpclient.executeMethod(method);

			if (statusCode != HttpStatus.SC_OK) {
				System.out.println(statusCode + ": " + method.getStatusLine());
			} else {
				System.out.println(new String(method.getResponseBody(), "UTF-8"));
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}

	}
}

获取当前自定义菜单内容:

package weixin;

import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;

import weixin.MySecureProtocolSocketFactory;

public class ClickMenuGet1 {

	public static void main(String[] args) {
		HttpClient httpclient;
		ProtocolSocketFactory fcty;
		String access_token;
		String url;
		GetMethod method;
		int statusCode;

		httpclient = new HttpClient();
		access_token = "${access_token}";
		url = "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=" + access_token;

		//https 证书
		fcty = new MySecureProtocolSocketFactory();
		Protocol.registerProtocol("https", new Protocol("https", fcty, 443));

		// get调用
		method = new GetMethod(url);
		method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));

		try {
			statusCode = httpclient.executeMethod(method);

			if (statusCode != HttpStatus.SC_OK) {
				System.out.println(statusCode + ": " + method.getStatusLine());
			} else {
				System.out.println(new String(method.getResponseBody(), "UTF-8"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
		}

	}
}

代码中用到的工具类和jar在之前的文章 http://blog.csdn.net/yuyang2013/article/details/10001379 中有。


创建完毕结果如下图:

微信公众平台开发[6]-开发模式-高级功能-自定义菜单的管理_第1张图片


你可能感兴趣的:(微信公众平台-Java)