微信公众号开发之用户分组
生活随笔
收集整理的這篇文章主要介紹了
微信公众号开发之用户分组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
具體參照微信官方文檔。
創建用戶分組
/*** 創建分組* @param groupName 分組名* @return Integer 分組ID*/ public static Integer createPrizeDrawGroup(String groupName){String url = "https://api.weixin.qq.com/cgi-bin/groups/create?access_token="+getGlobalToken();JSONObject json = new JSONObject();JSONObject jsonParent = new JSONObject();json.put("name", groupName);jsonParent.put("group", json);JSONObject demoJson = null;try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("POST"); // 必須是POST方式請求 http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true);http.getOutputStream().write(jsonParent.toString().getBytes());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"); demoJson = JSONObject.fromObject(message);//System.out.println("JSON字符串:"+demoJson); is.close(); } catch (Exception e) { e.printStackTrace(); }return demoJson.getJSONObject("group").getInt("id"); }批量移動用戶分組
/*** 移動用戶分組* @param list openID list* @param id 分組ID* @return 操作成功:true 失敗:false*/ public boolean updateGroup(List<String> list,Integer id){String url = "https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token="+getGlobalToken();JSONObject json = new JSONObject();json.put("openid_list", list);json.put("to_groupid", id);JSONObject demoJson = null;try { URL urlGet = new URL(url); HttpURLConnection http = (HttpURLConnection) urlGet.openConnection(); http.setRequestMethod("POST"); http.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); http.setDoOutput(true); http.setDoInput(true); http.getOutputStream().write(json.toString().getBytes());//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"); demoJson = JSONObject.fromObject(message); System.out.println("JSON字符串:"+demoJson); is.close(); } catch (Exception e) { e.printStackTrace(); }return demoJson.getInt("errcode")==1?true:false; }我有空再來添加詳細描述吧!
總結
以上是生活随笔為你收集整理的微信公众号开发之用户分组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell脚本编程基础(上)
- 下一篇: 代码战争之盐碱地