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 Settings 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-mode 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 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;
}
/**
* Create Menu
* @Title: CreateMenu
* @Description: Create menu
* @param @return
* @param @throws ioexception settings 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 list \", \ "key\": \ "01_ waiting\ "},{\" "type\": \ "Click\", "name\": \ "work orders \", "key\": \ "02_finish\"},{\ "type\": \ "Click\", "name\": \ "My work list \", \ "key\": \ "03_myjob\"},{\ "type\": \ "Click\", "name\": \ "Bulletin message box \", \ "key\": \ "04_messagebox\"},{\ "type\": \ "Click\", "" Name\ ": \ check-in \", \ "key\": \ "05_sign\"}]} ";
Instead, replace it with the structure you want.
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 30 seconds
Http.connect ();
OutputStream os= Http.getoutputstream ();
Os.write (Menu.getbytes ("UTF-8"));//Incoming 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 "returned information" +message;
catch (Malformedurlexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
Return "CreateMenu failure";
}
/**
* Delete current Menu
* @Title: DeleteMenu
* @Description: Delete current Menu
* @param @return Settings 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 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 information:" +message;
catch (Malformedurlexception e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
Return "DeleteMenu failure";
}
public static void Main (string[] args) {
System.out.println (CreateMenu ());
}
}