使用Java进行微信开发 4 创建自定义菜单

1 方式一:不写代码,使用微信公众平台接口调试工具来创建菜单

{

    "button": [

        {

            "name": "百度", 

            "type": "view", 

            "url": "http://www.baidu.com"

        }, 

        {

            "name": "菜单", 

            "sub_button": [

                {

                    "key": "text", 

                    "name": "回复图文", 

                    "type": "click"

                }, 

                {

                    "name": "百度", 

                    "type": "view", 

                    "url": "http://www.baidu.com"

                }

            ]

        }, 

        {

            "key": "text", 

            "name": "回复图文", 

            "type": "click"

        }

    ]

}

2 方式二:写代码创建菜单

ViewButton


package com.iris.wechat.menu;
/**
 * 视图型菜单事件
 * @author ifeng
 *
 */
public class ViewButton {
	
    private String type;
    private String name;
    private String url;
    
	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 getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
  
}

ClickButton


package com.iris.wechat.menu;

public class ClickButton {
  private String type;
  private String name;
  private String key;
  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;
  }
    
}


CreateMenu

package com.iris.wechat.menu;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class CreateMenu {
	public static void main(String[] args){
		
	String accessToken = null;
	 try {
		accessToken = AccessToken.getAccessToken();	
	} catch (Exception e) {
		System.out.println("fail to get AccessToken");
	}
	
	if (accessToken !=null ){
		ClickButton cbt=new ClickButton();
        cbt.setKey("image");
        cbt.setName("回复图片1");
        cbt.setType("click");
         
         
        ViewButton vbt=new ViewButton();
        vbt.setUrl("http://irisfeng.natapp1.cc/WechatValidation/toSAP.action");
        vbt.setName("ToSAP");
        vbt.setType("view");
         
        JSONArray sub_button=new JSONArray();
        sub_button.put(cbt);
        sub_button.put(vbt);
         
         
        JSONObject buttonOne=new JSONObject();
        buttonOne.put("name", "菜单");
        buttonOne.put("sub_button", sub_button);
         
        JSONArray button=new JSONArray();
        button.put(vbt);
        button.put(buttonOne);
        button.put(cbt);
         
        JSONObject menujson=new JSONObject();
        menujson.put("button", button);
        System.out.println(menujson);
        
        String postURL = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessToken;
        try {
		  URL url = new URL(postURL);
	      HttpURLConnection connection = (HttpURLConnection)url.openConnection();
			
	      connection.setRequestMethod("POST");
	      connection.setDoOutput(true);
	      connection.setDoInput(true);
	      connection.connect();
	
	      OutputStream outputStream = connection.getOutputStream();
	      outputStream.write(menujson.toString().getBytes("UTF-8"));
	      outputStream.flush();
	      outputStream.close();
	
	      InputStream inputStream = connection.getInputStream();
	      int size =inputStream.available();
	      byte[] bs =new byte[size];
	      inputStream.read(bs);
	      String message=new String(bs,"UTF-8");
	
	      System.out.println("message="+message);
		} catch (MalformedURLException e) {
					e.printStackTrace();
		} catch (IOException e) {
		
			e.printStackTrace();
		}

	}
	
	}
	
	
//	public static String getMenuStr() throws JSONException{
//        JSONObject firstLevelMenu = JSONObject.fromObject("");//一级菜单
//        JSONArray firstLevelMenuArray = new JSONArray();//一级菜单列表
//
//
//        //一级菜单内容1
//        JSONObject firstLevelMenuContext1 = new JSONObject();
//        firstLevelMenuContext1.put("type", "click");
//        firstLevelMenuContext1.put("name", "歌曲");
//        firstLevelMenuContext1.put("key", "V1001_TODAY_MUSIC");
//
//        //一级菜单内容2
//        JSONObject firstLevelMenuContext2 = new JSONObject();
//        //一级菜单内容2的二级菜单列表
//        JSONArray firstLevelMenuContext2Array = new JSONArray();
//        //一级菜单内容2的二级菜单内容1
//        JSONObject jsonObject1 = new JSONObject();
//        jsonObject1.put("type", "click");
//        jsonObject1.put("name", "歌曲");
//        jsonObject1.put("key", "V1001_TODAY_MUSIC");
//        //一级菜单内容2的二级菜单内容2
//        JSONObject jsonObject2 = new JSONObject();
//        jsonObject2.put("type", "view");
//        jsonObject2.put("name", "视频");
//        jsonObject2.put("url", "http://www.randzh.cn");
//        firstLevelMenuContext2Array.put(jsonObject1);
//        firstLevelMenuContext2Array.put(jsonObject2);
//        firstLevelMenuContext2.put("name", "菜单");
//        firstLevelMenuContext2.put("sub_button", firstLevelMenuContext2Array);
//
//        //一级菜单内容3
//        JSONObject firstLevelMenuContext3 = new JSONObject();
//        firstLevelMenuContext3.put("type", "click");
//        firstLevelMenuContext3.put("name", "视频");
//        firstLevelMenuContext3.put("key", "V1001_TODAY_MOVIE");
//
//
//        firstLevelMenuArray.put(firstLevelMenuContext1);
//        firstLevelMenuArray.put(firstLevelMenuContext2);
//        firstLevelMenuArray.put(firstLevelMenuContext1);
//
//
//        firstLevelMenu.put("button", firstLevelMenuArray);
//       
//        String str = firstLevelMenu.toString();
//        System.out.println("JSON str="+str);
//        return str;
//    }
//	
//	public static void createCustomMenu() throws Exception{
//        String custmMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
//
//        //获取access_token
//        String accessToken = AccessToken.getAccessToken();
//        custmMenuUrl = custmMenuUrl + accessToken;
//        System.out.println("===custmMenuUrl="+custmMenuUrl);
//        
//        URL url = new URL(custmMenuUrl);
//        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//
//        connection.setRequestMethod("POST");
//        connection.setDoOutput(true);
//        connection.setDoInput(true);
//        connection.connect();
//
//        OutputStream outputStream = connection.getOutputStream();
//        outputStream.write(getMenuStr().getBytes("UTF-8"));
//        outputStream.flush();
//        outputStream.close();
//
//        InputStream inputStream = connection.getInputStream();
//        int size =inputStream.available();
//        byte[] bs =new byte[size];
//        inputStream.read(bs);
//        String message=new String(bs,"UTF-8");
//
//        System.out.println("message="+message);
//}
}

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页