This article describes how to create a custom menu for the public platform in java. For more information about how to create a custom menu for the public platform in java, see the following article, for more information, see
The code is 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 setting 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"); // The request must be http in get mode. setRequestProperty ("Content-Type", "application/x-www-form-urlencoded"); http. setDoOutput (true); http. setDoInput (true); System. setProperty ("sun.net. client. defaultConnectTimeout "," 30000 "); // The connection times out for 30 seconds. setProperty ("sun.net. client. defaultReadTimeout "," 30000 "); // read timeout for 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 setting 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 ticket \", \ "key \": \ "01_WAITING \" },{ \ "type \": \ "click \", \ "name \": \ "open ticket \", \ "key \": \ "02_FINISH \" },{ \ "type \": \ "click \", \ "name \": \ "my tickets \", \ "key \": \ "03_MYJOB \"}, {\ "type \": \ "click \", \ "name \": \ "announcement message box \", \ "key \": \ "04_MESSAGEBOX \"}, {\ "type \": \ "click \", \ "name \": \ "sign in \", \ "key \": \ "05_SIGN \"}]} "; // replace it with the expected struct. replace it with 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 "); // The connection times out for 30 seconds. setProperty ("sun.net. client. defaultReadTimeout "," 30000 "); // read timeout for 30 seconds http. connect (); OutputStream OS = http. getOutputStream (); OS. write (menu. getBytes ("UTF-8"); // input parameter 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 the current Menu * @ Title: deleteMenu * @ Description: delete the current Menu * @ param @ return setting 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 "); // The connection times out for 30 seconds. setProperty ("sun.net. client. defaultReadTimeout "," 30000 "); // read timeout for 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 returned information:" + message;} catch (MalformedURLException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return "deleteMenu failed";} public static void main (String [] args) {System. out. println (createMenu ());}}
The above is the detailed content of the sample code for creating custom menus on the public platform using java. For more information, see other related articles on php Chinese network!