Java implementation of micro-trust public platform custom menu creation Example _java

Source: Internet
Author: User
Tags int size

Copy 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 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 ());
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.