Javaを使用してWeChatパブリックプラットフォームのカスタムメニューのサンプルコードを作成する
Mar 17, 2017 pm 03:23 PM
マイクロチャネルパブリックプラットフォーム
この記事では主に Java を使用して WeChat パブリック プラットフォームを?qū)g裝する方法を紹介しますカスタム メニュー 作成のサンプル コードを必要とする友人は參照してください。
コードは次のとおりです:
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 設(shè)定文件 * @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");//連接超時(shí)30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時(shí)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 設(shè)定文件 * @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\"}]}]}"; //此處改為自己想要的結(jié)構(gòu)體,替換即可 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");//連接超時(shí)30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時(shí)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 失敗"; } /** * 刪除當(dāng)前Menu * @Title: deleteMenu * @Description: 刪除當(dāng)前Menu * @param @return 設(shè)定文件 * @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");//連接超時(shí)30秒 System.setProperty("sun.net.client.defaultReadTimeout", "30000"); //讀取超時(shí)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を使用してWeChatパブリックプラットフォームのカスタムメニューのサンプルコードを作成するの詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國語 Web サイトの他の関連記事を參照してください。
このウェブサイトの聲明
この記事の內(nèi)容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰屬します。このサイトは、それに相當(dāng)する法的責(zé)任を負(fù)いません。盜作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡(luò)ください。

ホットAIツール

Undress AI Tool
脫衣畫像を無料で

Undresser.AI Undress
リアルなヌード寫真を作成する AI 搭載アプリ

AI Clothes Remover
寫真から衣服を削除するオンライン AI ツール。

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事
グラスワンダービルドガイド|ウマゴサメはかなりダービーです
1 か月前
By Jack chen
<??>:森の99泊 - すべてのバッジとそれらのロックを解除する方法
4週間前
By DDD
Uma Musume Pretty Derby Bannerスケジュール(2025年7月)
1 か月前
By Jack chen
船と墓のためのRimworld Odyssey溫度ガイド
3週間前
By Jack chen
Windowsセキュリティは空白であるか、オプションを表示しません
1 か月前
By 下次還敢

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中國語版
中國語版、とても使いやすい

ゼンドスタジオ 13.0.1
強(qiáng)力な PHP 統(tǒng)合開発環(huán)境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)