انسخ رمز الكود كما يلي:
import java.io.IOException;
استيراد java.io.InputStream؛
استيراد java.io.OutputStream؛
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
استيراد java.net.URL؛
import org.json.JSONObject;
قائمة الفئة العامة Util {
/**
* احصل على ACCESS_TOKEN
* @Title: getAccess_token
*@الوصف: احصل على ACCESS_TOKEN
*param @return ملف التكوين
*return نوع إرجاع السلسلة
* @رميات
*/
سلسلة ثابتة خاصة getAccess_token(){
سلسلة معرف التطبيق = ""؛
سلسلة APPSECRET = ""؛
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ APPID + "&secret=" +APPSECRET;
String accessToken = null;
يحاول {
URL urlGet = عنوان URL الجديد(url);
HttpURLConnection http = (HttpURLConnection) urlGet.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();
InputStream هو =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);
} قبض (الاستثناء ه) {
printStackTrace();
}
إرجاع رمز الوصول؛
}
/**
* إنشاء القائمة
* @Title: createMenu
* @الوصف: إنشاء القائمة
* @param @return
* @param @throws ملف التكوين IOException
*return نوع الإرجاع int
* @رميات
*/
سلسلة ثابتة عامة createMenu() {
قائمة السلسلة = "{/"button/":[{/"type/":/"click/"،/"name/":/"MENU01/"،/"key/":/"1/"},{/" النوع/":/"انقر/"،/"الاسم/":/"الاستعلام عن الطقس/"،/"مفتاح/":/" شيان/"},{/"name/":/"العمل اليومي/",/"sub_button/":[{/"type/":/"click/",/"name/":/"إلى -اعمل منفردًا/"،/"key/":/"01_WAITING/"},{/"type/":/"cl ick/"،/"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();
إجراء السلسلة = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token;
يحاول {
عنوان URL = عنوان URL الجديد(إجراء);
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"));// تمرير المعلمات
os.flush();
os.Close();
InputStream هو =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
إرجاع "معلومات الإرجاع"+رسالة؛
} التقاط (MalformedURLException e) {
printStackTrace();
} قبض (IOException ه) {
printStackTrace();
}
إرجاع "فشل إنشاء القائمة"؛
}
/**
* حذف القائمة الحالية
*@العنوان: حذف القائمة
* @Description: حذف القائمة الحالية
*param @return ملف التكوين
*return نوع إرجاع السلسلة
* @رميات
*/
سلسلة حذف ثابتة عامة ()
{
String access_token= getAccess_token();
إجراء السلسلة = "https://api.weixin.qq.com/cgi-bin/menu/delete? access_token = "+access_token؛
يحاول {
عنوان URL = عنوان URL الجديد(إجراء);
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 هو =http.getInputStream();
int size =is.available();
byte[] jsonBytes =new byte[size];
is.read(jsonBytes);
String message=new String(jsonBytes,"UTF-8");
إرجاع "deleteMenu يُرجع الرسالة:"+message;
} التقاط (MalformedURLException e) {
printStackTrace();
} قبض (IOException ه) {
printStackTrace();
}
إرجاع "فشل حذف القائمة"؛
}
public static void main(String[] args) {
System.out.println(createMenu());
}
}