微信开发系列(三)_编写服务器程序——生成菜单

菜单的生成是不需要服务器,只需要编写代码运行一次就可以生成 ()

  生成菜单所要向微信端传输的数据为 AccessToken(可通过appID和AppSecret获取),和Json格式的菜单


微信开发文档网址(点击打开链接

一:首先编写菜单的实体类Caidan.java

package com.weixin.util;


import java.util.List;


public class CaiDan {


	public static final String CLICK = "click";
	public static final String VIEW = "view";
	
	private List<CaiDan> sub_button = null;// 否  二级菜单数组,个数应为1~5个  类型依旧是CaiDan类型
	private String type = null;// 是  菜单的响应动作类型  
	private String name = null;// 是  菜单标题,不超过16个字节,子菜单不超过40个字节  
	private String key = null;// click等点击类型必须  菜单KEY值,用于消息接口推送,不超过128字节  
	private String url = null;// view类型必须  网页链接,用户点击菜单可打开链接,不超过256字节  


	public CaiDan() {
		super();
	}
	
	public CaiDan(String type, String name,	String key, String url) {
		super();
		this.type = type;
		this.name = name;
		this.key = key;
		this.url = url;
	}
	public List<CaiDan> getSub_button() {
		return sub_button;
	}
	public void setSub_button(List<CaiDan> sub_button) {
		this.sub_button = sub_button;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
}

二:根据项目的要求编写合适的菜单


package com.weixin.util;
import java.util.ArrayList;
import java.util.List;

import com.weixin.util.CaiDan;
import com.weixin.util.AccessToken;
import com.weixin.util.WX_CaiDan;
import com.weixin.util.WX_Token;


public class CaiDanUtil {
    public   List<CaiDan> caiDanList;
	public List<CaiDan> getCaiDanList() {
		main();
		return caiDanList;
	}
	public  void main() {
		System.out.println("test");
		AccessToken accessToken = WX_Token.getAccessTokenByNet();//<span style="color:#ff0000;">获取accessToken,在下一节会介绍到</span>
		
		//菜单list
		 caiDanList = new ArrayList<CaiDan>();
		
		//第一个一级菜单
		CaiDan sub_caidan0 = new CaiDan();		
		List<CaiDan> sub_caiDanList0 = new ArrayList<CaiDan>();
		
		//第一个一级菜单,第一个二级菜单 
		CaiDan caidan0_0 = new CaiDan();
		caidan0_0.setName("西瓜");
		caidan0_0.setType(CaiDan.CLICK);
		caidan0_0.setKey("0_0");
		sub_caiDanList0.add(caidan0_0);
		
		//第一个一级菜单,第二个二级菜单
		CaiDan caidan0_1 = new CaiDan();
		caidan0_1.setName("香瓜");
		caidan0_1.setType(CaiDan.CLICK);
		caidan0_1.setKey("0_1");
		sub_caiDanList0.add(caidan0_1);
				
		//第一个一级菜单,第三个二级菜单
		CaiDan caidan0_2 = new CaiDan();
		caidan0_2.setName("木瓜");
		caidan0_2.setType(CaiDan.CLICK);
		caidan0_2.setKey("0_2");
		sub_caiDanList0.add(caidan0_2);
		sub_caidan0.setName("果蔬");
		sub_caidan0.setSub_button(sub_caiDanList0);
		caiDanList.add(sub_caidan0);
		
		
		/*第二个一级菜单*/
		CaiDan sub_caidan1 = new CaiDan();
		sub_caidan1.setName("肉类");
		List<CaiDan> sub_caiDanList1 = new ArrayList<CaiDan>();
		
		/*第二个一级带单,第一个二级菜单*/
		CaiDan caidan1_0 = new CaiDan();
		caidan1_0.setName("羊肉");
		caidan1_0.setType(CaiDan.CLICK);
		caidan1_0.setKey("1_0");		
		sub_caiDanList1.add(caidan1_0);
		
		/*第二个一级菜单,第二个二级菜单*/
		CaiDan caidan1_1 = new CaiDan();
		caidan1_1.setName("驴肉");
		caidan1_1.setType(CaiDan.CLICK);
		caidan1_1.setKey("1_1");
		sub_caiDanList1.add(caidan1_1);
		
		/*第二个一级菜单。第三个二级菜单*/
		CaiDan caiDan1_2 = new CaiDan();
		caiDan1_2.setName("鸭肉");
		caiDan1_2.setType(CaiDan.CLICK);
		caiDan1_2.setKey("1_2");
		sub_caiDanList1.add(caiDan1_2);
		
		sub_caidan1.setSub_button(sub_caiDanList1);
		caiDanList.add(sub_caidan1);
		
		/*第三个一级菜单*/
		CaiDan caiDan = new CaiDan();
		caiDan.setName("说明");
		caiDan.setType(CaiDan.VIEW);
		caiDan.setKey("text");
		caiDan.setUrl("http://0.baidu.com/?soft=134&tn=11000002");
		caiDanList.add(caiDan);
		
		/*create*/
		//System.out.println(WX_CaiDan.create(accessToken.getAccess_token(), caiDanList));
		
		/*报文*/
		//System.out.println(WX_CaiDan.getStr(caiDanList));
		
		/*query*/
		//System.out.println(WX_CaiDan.query(accessToken.getAccess_token()));
		
		/*delete*/
		//System.out.println(WX_CaiDan.delete(accessToken.getAccess_token()));
	}

}

三:将菜单转化为微信端可读取的Json格式(以下代码是通用转换方法)

public static String getStr(List<CaiDan> caiDanList){
		
		StringBuffer strb = new StringBuffer();
		strb.append("{") ;
		strb.append("\"button\":[");     

		CaiDan caiDian = null;
		
		CaiDan caiDian_ = null;
		
		for(int x = 0 ; x < caiDanList.size() ; x++){
			
			caiDian = caiDanList.get(x);
			
			if(null==caiDian.getSub_button()){
				strb.append(" {");
				strb.append("   \"type\":\""+caiDian.getType()+"\",");
				strb.append("   \"name\":\""+caiDian.getName()+"\",");
				if(CaiDan.CLICK.equals(caiDian.getType())){
					strb.append("   \"key\":\""+caiDian.getKey()+"\" ");				
				}else if(CaiDan.VIEW.equals(caiDian.getType())){
					strb.append("   \"url\":\""+caiDian.getUrl()+"\" ");
				}			
				strb.append(" }");
			}else{
				
				strb.append("{");
				strb.append("\"name\":\""+caiDian.getName()+"\",");
				strb.append("\"sub_button\":[");

				 for(int xx = 0 ; xx < caiDian.getSub_button().size() ; xx++){
					 
					 caiDian_ = caiDian.getSub_button().get(xx);
					 
					 strb.append(" {");
					 strb.append("   \"type\":\""+caiDian_.getType()+"\",");
					 strb.append("   \"name\":\""+caiDian_.getName()+"\",");
					 if(CaiDan.CLICK.equals(caiDian_.getType())){
						 strb.append("   \"key\":\""+caiDian_.getKey()+"\" ");				
					 }else if(CaiDan.VIEW.equals(caiDian_.getType())){
						 strb.append("   \"url\":\""+caiDian_.getUrl()+"\" ");
					 }			
					 strb.append(" }");
					 
					 if(xx+1!=caiDian.getSub_button().size()){
						strb.append(",");
					}
					 
				 }
			           
			    strb.append("]");
			    strb.append("}");

			}
			
			if(x+1!=caiDanList.size()){
				
				strb.append(",");
				
			}
			
		}
	
		strb.append(" ] ");
		strb.append(" } ");
		
		return strb.toString();
		
	}
	

四:生成菜单

	public static String create(String access_token , List<CaiDan> caiDanList){
		
		/*返回的字符串*/
		StringBuffer strb = new StringBuffer();
		
		/*连接对象*/
		HttpURLConnection connection = null;
		
		try {
			
			URL url = new URL("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token);
			
			connection = (HttpURLConnection)url.openConnection();
			
			/*设置POST*/
			connection.setDoOutput(true);
			connection.setRequestMethod("POST");
			
			/*得到输出流*/
			OutputStream out = connection.getOutputStream();
			
			/*封装OutputStreamWriter*/
			OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
			
			/*封装BufferedWriter*/
			BufferedWriter bwriter = new BufferedWriter(writer);
			
			/*写出*/
			bwriter.write(getStr(caiDanList));
			
			/*flush*/
			bwriter.flush();
			
			/*得到输入流*/
			InputStream in = connection.getInputStream();
			
			/*封装InputStreamReader*/
			InputStreamReader reader = new InputStreamReader(in);
			
			/*封装BufferedReader*/
			BufferedReader breader = new BufferedReader(reader);
			
			/*读取*/
			String str = null;
			
			while(null != (str = breader.readLine())){
				
				strb.append(str);
				
			}
			
			/*关闭out*/
			out.close();
			
			/*关闭writer*/
			writer.close();
			
			/*关闭bwriter*/
			bwriter.close();
			
			/*关闭in*/
			in.close();
			
			/*关闭*/
			reader.close();
			
			/*关闭breader*/
			breader.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(null != connection){
			
			connection.disconnect();
			
		}
		
		return strb.toString();
		
	}


最后只要运行create程序就ok
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值