java微信接口之二—獲取用戶組


一、微信獲取用戶組接口簡介

  1、請求

  該請求也是GET方式請求。請求的url格式如下:

  https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN

  其中ACCESS_TOKEN是之前我們獲取到的。

  2、響應

  該響應也是以json方式返回的

  正確的時候返回的數據:

{
    "groups": [
        {
            "id": 0, 
            "name": "未分組", 
            "count": 72596
        }, 
        {
            "id": 1, 
            "name": "黑名單", 
            "count": 36
        }, 
        {
            "id": 2, 
            "name": "星標組", 
            "count": 8
        } 
  ]
} 

  groups,對應返回的用戶組信息數組;id,用戶組id;name,用戶組名稱;count,用戶數量。

  錯誤的時候返回的數據:{"errcode":40013,"errmsg":"invalid appid"}

  errcode,為錯誤代碼,errmsg為錯誤信息

  具體api可查看文檔:http://mp.weixin.qq.com/wiki/index.php?title=%E5%88%86%E7%BB%84%E7%AE%A1%E7%90%86%E6%8E%A5%E5%8F%A3

二、關於java代碼的調用

  這里與獲取access_token一樣使用的都是apache的http組件httpcomponents-client。

三、代碼實現

 1 import java.util.Arrays;
 2 
 3 import org.apache.http.HttpEntity;
 4 import org.apache.http.HttpResponse;
 5 import org.apache.http.HttpStatus;
 6 import org.apache.http.client.HttpClient;
 7 import org.apache.http.client.methods.HttpGet;
 8 import org.apache.http.impl.client.DefaultHttpClient;
 9 import org.apache.http.util.EntityUtils;
10 
11 import com.google.gson.JsonArray;
12 import com.google.gson.JsonObject;
13 import com.google.gson.JsonParser;
14 
15 public class Test
16 {
17     public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";// 獲取access
18     public static final String GET_USER_GROUP = "https://api.weixin.qq.com/cgi-bin/groups/get"; // url
19     public static final String APP_ID = "wxa549b28c24cf341e";
20     public static final String SECRET = "78d8a8cd7a4fa700142d06b96bf44a37";
21 
22     /**
23      * 獲取用戶組信息
24      * 
25      * @param url
26      *            訪問url
27      * @param token
28      *            access_token
29      * @return id字符串,每個id以,分割
30      */
31     public static String getGroups(String url, String token)
32     {
33         String groupurl = String.format("%s?access_token=%s", url, token);
34         System.out.println(groupurl);
35         HttpClient client = new DefaultHttpClient();
36         HttpGet get = new HttpGet(groupurl);
37         String result = null;
38         try
39         {
40             HttpResponse res = client.execute(get);
41             String responseContent = null; // 響應內容
42             HttpEntity entity = res.getEntity();
43             responseContent = EntityUtils.toString(entity, "UTF-8");
44             JsonParser jsonparer = new JsonParser();// 初始化解析json格式的對象
45             JsonObject json = jsonparer.parse(responseContent)
46                     .getAsJsonObject();// 將json字符串轉換為json對象
47             if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK)// 成功返回消息
48             {
49                 if (json.get("errcode") == null)// 不存在錯誤消息,成功返回
50                 {
51                     JsonArray groups = json.getAsJsonArray("groups"); // 返回對象數組
52                     StringBuffer buffer = new StringBuffer();
53                     for (int i = 0; i < groups.size(); i++)
54                     {
55                         buffer.append(groups.get(i).getAsJsonObject().get("id")
56                                 .getAsString()
57                                 + ",");
58                     }
59                     result = buffer.toString();
60                 }
61             }
62         }
63         catch (Exception e)
64         {
65             e.printStackTrace();
66         }
67         finally
68         { // 關閉連接 ,釋放資源
69             client.getConnectionManager().shutdown();
70             return result;
71         }
72     }
73 
74     public static void main(String[] args) throws Exception
75     {
76         System.out.println("=========1獲取token=========");
77         String accessToken = getToken(GET_TOKEN_URL, APP_ID, SECRET);// 獲取token在微信之一中的方法獲取token
78         if (accessToken != null)// token成功獲取
79         {
80             String ids = getGroups(GET_USER_GROUP, accessToken);
81             if (ids != null)
82             {
83                 String[] idarray = ids.split(",");// 用戶組id數組
84                 System.out.println(ids);
85             }
86         }
87     }
88 }

 

  成功調用或


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM