java實現(xiàn)微信公眾平臺自定義菜單的創(chuàng)建示例
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 {
/**
* 獲得ACCESS_TOKEN
* @Title: getAccess_token
* @Description: 獲得ACCESS_TOKEN
* @param @return 設定文件
* @return String 返回類型
* @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"); //必須是get方式請求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");//連接超時30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時30秒
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;
}
/**
* 創(chuàng)建Menu
* @Title: createMenu
* @Description: 創(chuàng)建Menu
* @param @return
* @param @throws IOException 設定文件
* @return int 返回類型
* @throws
*/
public static String createMenu() {
String menu = "{\"button\":[{\"type\":\"click\",\"name\":\"MENU01\",\"key\":\"1\"},{\"type\":\"click\",\"name\":\"天氣查詢\",\"key\":\"西安\"},{\"name\":\"日常工作\",\"sub_button\":[{\"type\":\"click\",\"name\":\"待辦工單\",\"key\":\"01_WAITING\"},{\"type\":\"click\",\"name\":\"已辦工單\",\"key\":\"02_FINISH\"},{\"type\":\"click\",\"name\":\"我的工單\",\"key\":\"03_MYJOB\"},{\"type\":\"click\",\"name\":\"公告消息箱\",\"key\":\"04_MESSAGEBOX\"},{\"type\":\"click\",\"name\":\"簽到\",\"key\":\"05_SIGN\"}]}]}";
//此處改為自己想要的結構體,替換即可
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");//連接超時30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時30秒
http.connect();
OutputStream os= http.getOutputStream();
os.write(menu.getBytes("UTF-8"));//傳入?yún)?shù)
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 "返回信息"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "createMenu 失敗";
}
/**
* 刪除當前Menu
* @Title: deleteMenu
* @Description: 刪除當前Menu
* @param @return 設定文件
* @return String 返回類型
* @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");//連接超時30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時30秒
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返回信息:"+message;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "deleteMenu 失敗";
}
public static void main(String[] args) {
System.out.println(createMenu());
}
}
相關文章
Java中向文件寫入數(shù)據(jù)的幾種常見方式分享
在日常開發(fā)中,肯定離不開要和文件打交道,今天就簡單羅列一下平時比較常用的創(chuàng)建文件并向文件中寫入數(shù)據(jù)的幾種方式,文中有詳細的代碼示例供大家參考,具有一定的參考價值,需要的朋友可以參考下2023-10-10
SpringBoot Application事件監(jiān)聽的實現(xiàn)方案
這篇文章主要介紹了SpringBoot Application事件監(jiān)聽的實現(xiàn)方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05
Springboot在有鎖的情況下正確使用事務的實現(xiàn)代碼
這篇文章主要介紹了Springboot在有鎖的情況下如何正確使用事務,今天通過一個實驗給大家分析一下商品超賣問題,模擬場景分析通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2021-12-12
spring boot實現(xiàn)過濾器和攔截器demo
本篇文章主要介紹了spring boot實現(xiàn)過濾器和攔截器demo ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02

