Home WeChat Applet WeChat Development Creation of custom menu in Java WeChat development

Creation of custom menu in Java WeChat development

May 10, 2017 am 09:32 AM
java WeChat Custom menu

This article mainly introduces the tenth step of Java WeChat public platform development in detail, the creation and implementation of WeChat custom menu, which has certain reference value. Interested friends can refer to it

The function of custom menu can be edited directly in the background in our normal editing mode, but once we enter the development mode, our custom menu needs to be implemented by ourselves, so it may be a problem for those who are new to it. Some doubts, here I will talk about two ways to implement custom menus that we usually use in development mode: ① No need to write implementation code, directly use the web page testing tool Post jsonStringGenerate menu; ② is to use code to generate menu in our development! (Reference document: http://mp.weixin.qq.com/wiki/10/0234e39a2025342c17a7d23595c6b40a.html) There are two types of menus in the custom menu, one is the view's View menu , jump directly to the url page after clicking; there is also a click type, the backend gives different responses through the click event type; lateradded various The menus of special functions are essentially Click-type menus, so the generated rules are the same. The way to generate menus is to post json strings to the WeChat server to generate menus. The methods and rules for menu generation are described below!

(1) Use the webpageDebugTools to generate the menu

We connect through (mp.weixin.qq.com/debug/cgi-bin /apiinfo?t=index&type=%E8%87%AA%E5%AE%9A%E4%B9%89%E8%8F%9C%E5%8D%95&form=%E8%87%AA%E5%AE%9A %E4%B9%89%E8%8F%9C%E5%8D%95%E5%88%9B%E5%BB%BA%E6%8E%A5%E5%8F%A3%20/menu/create ) Enter Go to the web debugging tool, as shown below:

When we generate the menu here, we only need the valid token of our account and the json string. Here is the json The string can be modified by referring to the cases in the document. A case I give here is as follows:

{
 "button": [
  {
   "name": "博客", 
   "type": "view", 
   "url": "http://www.cuiyongzhi.com"
  }, 
  {
   "name": "菜单", 
   "sub_button": [
    {
     "key": "text", 
     "name": "回复图文", 
     "type": "click"
    }, 
    {
     "name": "博客", 
     "type": "view", 
     "url": "http://www.cuiyongzhi.com"
    }
   ]
  }, 
  {
   "key": "text", 
   "name": "回复图文", 
   "type": "click"
  }
 ]
}
Copy after login

We fill in the response token, click Check Problem, and if the return result is OK, it is ok, as follows:

At this point, we have completed using the web testing tool to generate the menu. Next, we will introduce the use of code to generate the menu!

(2) Use code to generate menus

We mentioned earlier that there are two types of events, view and click, in the menu. Here we first create a menu in the code Create two types of corresponding java entities. The view type creates the entity ViewButton.java as follows:

package com.cuiyongzhi.wechat.menu;
 
/**
 * ClassName: ViewButton
 * @Description: 视图型菜单事件
 * @author dapengniao
 * @date 2016年3月14日 下午5:31:38
 */
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;
 }
 
}
Copy after login

Similarly creates the click entity ClickButton.java as follows:

package com.cuiyongzhi.wechat.menu;
 
/**
 * ClassName: ClickButton
 * @Description: 点击型菜单事件
 * @author dapengniao
 * @date 2016年3月14日 下午5:31:50
 */
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;
 }
 
}
Copy after login

The purpose of creating two entities here is also It is convenient for us to encapsulate json in the custom menu. Here I encapsulate the same json format given above in the form of code, and call the interface to generate the custom menu and send it to the WeChat server. Simple The code is as follows:

package com.cuiyongzhi.wechat.menu;
 
import com.alibaba.fastjson.JSONObject;
import com.cuiyongzhi.wechat.util.HttpUtils;
 
import net.sf.json.JSONArray;
 
public class MenuMain {
 
 public static void main(String[] args) {
  
  ClickButton cbt=new ClickButton();
  cbt.setKey("image");
  cbt.setName("回复图片");
  cbt.setType("click");
   
   
  ViewButton vbt=new ViewButton();
  vbt.setUrl("http://www.cuiyongzhi.com");
  vbt.setName("博客");
  vbt.setType("view");
   
  JSONArray sub_button=new JSONArray();
  sub_button.add(cbt);
  sub_button.add(vbt);
   
   
  JSONObject buttonOne=new JSONObject();
  buttonOne.put("name", "菜单");
  buttonOne.put("sub_button", sub_button);
   
  JSONArray button=new JSONArray();
  button.add(vbt);
  button.add(buttonOne);
  button.add(cbt);
   
  JSONObject menujson=new JSONObject();
  menujson.put("button", button);
  System.out.println(menujson);
  //这里为请求接口的url +号后面的是token,这里就不做过多对token获取的方法解释
  String url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+"upeDW-2pWrHgLx3fGqgsvAvf-HkQBA--5uHOo9OW16uNdL9zNPnnuIN01UDFXh_5d-QdcnBxux9tXigFwm1z0SInbdkXEKa1pMhTqaZVxK7sCPj7421YQGI0v3evwiwiWALjAHASWH";
   
  try{
   String rs=HttpUtils.sendPostBuffer(url, menujson.toJSONString());
   System.out.println(rs);
  }catch(Exception e){
   System.out.println("请求错误!");
  }
  
 }
 
}
Copy after login

The basic process of the above code is to call the two entities of view and click to encapsulate the json string menujson, and finally call the send method to send the json to the Tencent server, but here you need to use the token generated by the account. I wrote it here directly (see how to obtain the token), and the final operation returns the result ok, as follows:

The function implementation of the custom menu is basically these. , the next article will talk about [the relationship between WeChat public platform (map.weixin.qq.com)/open platform (open.weixin.qq.com)/merchant platform (pay.weixin.qq.com)] I hope I can help you. Thank you for reading. If you have any questions, you can leave a message for discussion!

【Related recommendations】

1. WeChat public account platform source code download

2. WeChat voting source code

The above is the detailed content of Creation of custom menu in Java WeChat development. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

What is the difference between H5 page production and WeChat applets What is the difference between H5 page production and WeChat applets Apr 05, 2025 pm 11:51 PM

H5 is more flexible and customizable, but requires skilled technology; mini programs are quick to get started and easy to maintain, but are limited by the WeChat framework.

The difference between H5 and mini-programs and APPs The difference between H5 and mini-programs and APPs Apr 06, 2025 am 10:42 AM

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

PHP: The Foundation of Many Websites PHP: The Foundation of Many Websites Apr 13, 2025 am 12:07 AM

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

See all articles