600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 使用java实现微信公众平台自定义菜单的创建示例代码

使用java实现微信公众平台自定义菜单的创建示例代码

时间:2022-02-13 06:43:43

相关推荐

使用java实现微信公众平台自定义菜单的创建示例代码

微信小程序|微信开发

微信公众平台,公众平台自定义菜单

微信小程序-微信开发代码如下:

cc攻击器vc源码,用vscode打开指定文件,ubuntu网页全屏,如何是否安装tomcat,python爬虫全套,php去符号,蚌埠靠谱的seo推广,网站源码编辑器php,bt种子采集站源码模板lzw

import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import .HttpURLConnection;import .MalformedURLException;import .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./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(".client.defaultConnectTimeout", "30000");//连接超时30秒 System.setProperty(".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;} /** * 创建Menu * @Title: createMenu * @Description: 创建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./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(".client.defaultConnectTimeout", "30000");//连接超时30秒 System.setProperty(".client.defaultReadTimeout", "30000"); //读取超时30秒 http.connect(); OutputStream os= http.getOutputStream();os.write(menu.getBytes("UTF-8"));//传入参数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./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(".client.defaultConnectTimeout", "30000");//连接超时30秒System.setProperty(".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()); }}

imsdroid 源码下载,vscode一定要装在c盘吗,ubuntu扩展卷,tomcat项目文件为空,外国爬虫游戏,js提交数据到php,林州市seo优化哪家的正规lzw

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。