자바 위 챗 인터페이스 2 - 사용자 그룹 가 져 오기

13697 단어 자바
1. 위 챗 사용자 그룹 인터페이스 프로필 가 져 오기
1. 부탁
이 요청 도 GET 방식 으로 요청 했다.요청 한 url 형식 은 다음 과 같 습 니 다:
  https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN
그 중 ACCESS토 큰 은 전에 우리 가 얻 은 거 야.
2. 호응
이 응답 도 제 이 슨 방식 으로 돌 아 왔 습 니 다.
올 바른 시간 에 되 돌아 오 는 데이터:
{

    "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
2. 자바 코드 호출
여기 와 획득 accesstoken 처럼 apache 의 http 구성 요소 httpcomponents - client 를 사용 합 니 다.
3. 코드 구현
 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 }

 
성공 호출 또는

좋은 웹페이지 즐겨찾기