微信创建自定义菜单代码

自8月5号微信5.0上线后,微信公众平台服务号可以创建自定义菜单了。经过整理网友的材料,自定义菜单的实现代码如下:

 

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;


public class wx_menu
{
	  
	public static void main(String[] args){
			String url = https://api.weixin.qq.com/cgi-bin/menu/create?access_token=你的TOKEN";
			/**
			 * 设置菜单
			 * 在为什么用\"你懂得,这是java代码
			 */
			String responeJsonStr = "{"+
					"\"button\":["+
						"{\"name\":\"菜单名称1\","+
						"\"type\":\"click\"," +
						"\"key\":\"V01_S01\"" +
						"},"+
						"{\"name\":\"菜单名称2\","+
						"\"type\":\"click\"," +
						"\"key\":\"V02_S01\"" +
						"},"+
						"{\"name\":\"菜单名称1\","+
						"\"type\":\"click\"," +
						"\"key\":\"V03_S01\"" +
						"}"+
					"]"+
				"}";
			
						
			HttpClient client = new HttpClient();
			PostMethod post = new PostMethod(url);
			post.setRequestBody(responeJsonStr);
			post.getParams().setContentCharset("utf-8");
			//发送http请求
			String respStr = "";
			try {
				client.executeMethod(post);
				respStr = post.getResponseBodyAsString();
			} catch (HttpException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			System.out.println(responeJsonStr);
			System.out.println(respStr);
		}
	
}


 

另外,记得导入commons-httpclient-3.0.1.jar


 

 

posted @ 2013-08-08 21:49  pangbangb  阅读(693)  评论(0编辑  收藏  举报