Copy the code code as follows:
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 org.json.JSONObject;
public class MenuUtil {
/**
* Get ACCESS_TOKEN
* @Title: getAccess_token
* @Description: Get ACCESS_TOKEN
* @param @return configuration file
* @return String return type
* @throws
*/
private static String getAccess_token(){
String APPID="";
String APPSECRET="";
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APPID + "&secret=" +APPSECRET;
String accessToken = null;
try {
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); //Must be a get request
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//Connection timeout 30 seconds
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //Read timeout is 30 seconds
http.connect();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
JSONObject demoJson = new JSONObject(message);
accessToken = demoJson.getString("access_token");
System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
return accessToken;
}
/**
*CreateMenu
* @Title: createMenu
* @Description: Create Menu
* @param @return
* @param @throws IOException configuration file
* @return int return type
* @throws
*/
public static String createMenu() {
String menu = "{/"button/":[{/"type/":/"click/",/"name/":/"MENU01/",/"key/":/"1/"},{/" type/":/"click/",/"name/":/"Weather query/",/"key/":/" Xi'an/"},{/"name/":/"Daily work/",/"sub_button/":[{/"type/":/"click/",/"name/":/"To-do work single/",/"key/":/"01_WAITING/"},{/"type/":/"cl ick/",/"name/":/"Work order processed/",/"key/":/"02_FINISH/"},{/"type/":/"click/",/"name/" :/"My work order/",/"key/":/"03_MYJOB/"},{/"type/": /"click/",/"name/":/"Announcement Message Box/",/"key/":/"04_MESSAGEBOX/"},{/"type/":/"click/",/"name/ ":/"Sign in/",/"key/":/"05_SIGN/"}]}]}";
//Change here to the structure you want and just replace it
String access_token= getAccess_token();
String action = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//Connection timeout 30 seconds
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //Read timeout is 30 seconds
http.connect();
OutputStream os= http.getOutputStream();
os.write(menu.getBytes("UTF-8"));//Pass in parameters
os.flush();
os.close();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
return "return information"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "createMenu failed";
}
/**
* Delete current Menu
* @Title: deleteMenu
* @Description: Delete the current Menu
* @param @return configuration file
* @return String return type
* @throws
*/
public static String deleteMenu()
{
String access_token= getAccess_token();
String action = "https://api.weixin.qq.com/cgi-bin/menu/delete? access_token="+access_token;
try {
URL url = new URL(action);
HttpURLConnection http = (HttpURLConnection) url.openConnection();
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//Connection timeout 30 seconds
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //Read timeout is 30 seconds
http.connect();
OutputStream os= http.getOutputStream();
os.flush();
os.close();
InputStream is =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
return "deleteMenu returns message:"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "deleteMenu failed";
}
public static void main(String[] args) {
System.out.println(createMenu());
}
}