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 }

 

  成功调用或

你可能感兴趣的:(java)