Java通過QQ群的API讀取群組狀態的程式碼
阿新 • • 發佈:2018-11-17
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow
也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!
有好多群,懶得隨時更新群的狀態,特別是當前人數這個經常變的東西。這裡根據QQ的一個API程式,讀取狀態,直接看看程式碼吧。
讀取的介面程式和測試程式碼!
- package
- import java.io.Reader;
- import java.io.StringReader;
- import java.util.List;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.input.SAXBuilder;
- /**
- * 讀取的介面程式。
- *
- * @author 老紫竹 www.laozizhu.com
- */
- public class QQGroupBaseInfoAPI {
- public static QQGroupBaseInfo get(String id) {
- try {
- String textXml = PageService.getPage(
- char c = 0x1d;
- String s = String.valueOf(c);
- textXml = textXml.replaceAll(s, "");
- SAXBuilder builder = new SAXBuilder();
- Document doc = null;
- Reader in = new StringReader(textXml);
- doc = builder.build(in);
- Element root = doc.getRootElement(); // 拿到 date
- List date = root.getChildren(); // 下一級資料
- Element result = (Element) date.get(0); // 結果標誌
- if (!"0".equals(result.getTextTrim())) {
- System.out.println("沒找到" + id);
- return null; // 沒找到
- }
- Element item = (Element) date.get(1);
- QQGroupBaseInfo o = new QQGroupBaseInfo();
- o.setLogo(MyHTMLDecoder.decode(item.getChildTextTrim("logo")));
- o.setMemo(MyHTMLDecoder.decode(item.getChildTextTrim("memo")));
- o.setMale(MyHTMLDecoder.decode(item.getChildTextTrim("male")));
- o.setFemale(MyHTMLDecoder.decode(item.getChildTextTrim("female")));
- o.setRank(MyHTMLDecoder.decode(item.getChildTextTrim("rank")));
- o.setGroupId(MyHTMLDecoder.decode(item.getChildTextTrim("groupid")));
- o.setGroupType(MyHTMLDecoder.decode(item.getChildTextTrim("grouptype")));
- o.setGroupOption(MyHTMLDecoder.decode(item.getChildTextTrim("logo")));
- o.setBrief(MyHTMLDecoder.decode(item.getChildTextTrim("brief")));
- o.setGroupClass(MyHTMLDecoder.decode(item.getChildTextTrim("groupclass")));
- o.setCreator(MyHTMLDecoder.decode(item.getChildTextTrim("creator")));
- o.setUin(MyHTMLDecoder.decode(item.getChildTextTrim("uin")));
- o.setFaceId(MyHTMLDecoder.decode(item.getChildTextTrim("faceid")));
- o.setCreateTime(MyHTMLDecoder.decode(item.getChildTextTrim("crttime")));
- o.setGroupStatus(MyHTMLDecoder.decode(item.getChildTextTrim("gpstatus")));
- o.setClassName(MyHTMLDecoder.decode(item.getChildTextTrim("class")));
- o.setFlag(Long.parseLong(item.getChildTextTrim("flag")));
- o.setTotal(Integer.parseInt(item.getChildTextTrim("total")));
- o.setGuestDeny(Integer.parseInt(item.getChildTextTrim("guestdeny")));
- o.setMemberMax(Integer.parseInt(item.getChildTextTrim("membermax")));
- o.setGroupFace(Integer.parseInt(item.getChildTextTrim("groupface")));
- o.setGroupName(MyHTMLDecoder.decode(item.getChildTextTrim("groupname")));
- o.setOpen(Integer.parseInt(item.getChildTextTrim("isopen")) == 1);
- o.setOpen(Integer.parseInt(item.getChildTextTrim("isopen")) == 1);
- o.setCloseExpire(Integer.parseInt(item.getChildTextTrim("iscloseexpire")) == 1);
- o.setSuperExpireDate(MyHTMLDecoder.decode(item.getChildTextTrim("superexpiredate")));
- return o;
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- }
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- QQGroupBaseInfo o = get("56763840");
- System.out.println(o.getTotal());
- System.out.println(o.getMemberMax());
- }
- }
讀取的介面資訊
- package com.laozizhu.blog.util;
- /**
- * QQ的基礎資訊。
- *
- * @author 老紫竹 www.laozizhu.com
- */
- public class QQGroupBaseInfo {
- // 圖示
- private String logo;
- // 公告
- private String memo;
- private String male;
- private String female;
- private String rank;
- // 群號
- private String groupId;
- private String groupType;
- private String groupOption;
- // 簡介
- private String brief;
- private String groupClass;
- private String groupClassEx;
- // 建立人
- private String creator;
- // 建立人的號碼
- private String uin;
- private String faceId;
- private String createTime;
- // 是否為高階群
- private String groupStatus;
- // 分類,比如軟體交流
- private String className;
- private long flag;
- // 當前總人數
- private int total;
- private int guestDeny;
- // 最高人數
- private int memberMax;
- private int groupFace;
- // 群名字
- private String groupName;
- private boolean open;
- private boolean mem;
- private boolean closeExpire;
- // 高階群過期日期
- private String superExpireDate;
- public String getLogo() {
- return logo;
- }
- public void setLogo(String logo) {
- this.logo = logo;
- }
- public String getMemo() {
- return memo;
- }
- public void setMemo(String memo) {
- this.memo = memo;
- }
- public String getGroupId() {
- return groupId;
- }
- public void setGroupId(String groupId) {
- this.groupId = groupId;
- }
- public String getGroupType() {
- return groupType;
- }
- public void setGroupType(String groupType) {
- this.groupType = groupType;
- }
- public String getGroupOption() {
- return groupOption;
- }
- public void setGroupOption(String groupOption) {
- this.groupOption = groupOption;
- }
- public String getGroupClass() {
- return groupClass;
- }
- public void setGroupClass(String groupClass) {
- this.groupClass = groupClass;
- }
- public String getGroupClassEx() {
- return groupClassEx;
- }
- public void setGroupClassEx(String groupClassEx) {
- this.groupClassEx = groupClassEx;
- }
- public String getBrief() {
- return brief;
- }
- public void setBrief(String brief) {
- this.brief = brief;
- }
- public String getCreator() {
- return creator;
- }
- public void setCreator(String creator) {
- this.creator = creator;
- }
- public String getUin() {
- return uin;
- }
- public void setUin(String uin) {
- this.uin = uin;
- }
- public String getClassName() {
- return className;
- }
- public void setClassName(String className) {
- this.className = className;
- }
- public long getFlag() {
- return flag;
- }
- public void setFlag(long flag) {
- this.flag = flag;
- }
- public int getTotal() {
- return total;
- }
- public void setTotal(int total) {
- this.total = total;
- }
- public int getGuestDeny() {
- return guestDeny;
- }
- public void setGuestDeny(int guestDeny) {
- this.guestDeny = guestDeny;
- }
- public int getMemberMax() {
- return memberMax;
- }
- public void setMemberMax(int memberMax) {
- this.memberMax = memberMax;
- }
- public int getGroupFace() {
- return groupFace;
- }
- public void setGroupFace(int groupFace) {
- this.groupFace = groupFace;
- }
- public String getGroupName() {
- return groupName;
- }
- public void setGroupName(String groupName) {
- this.groupName = groupName;
- }
- public boolean isOpen() {
- return open;
- }
- public void setOpen(boolean open) {
- this.open = open;
- }
- public boolean isMem() {
- return mem;
- }
- public void setMem(boolean mem) {
- this.mem = mem;
- }
- public boolean isCloseExpire() {
- return closeExpire;
- }
- public void setCloseExpire(boolean closeExpire) {
- this.closeExpire = closeExpire;
- }
- public String getMale() {
- return male;
- }
- public void setMale(String male) {
- this.male = male;
- }
- public String getFemale() {
- return female;
- }
- public void setFemale(String female) {
- this.female = female;
- }
- public String getRank() {
- return rank;
- }
- public void setRank(String rank) {
- this.rank = rank;
- }
- public String getFaceId() {
- return faceId;
- }
- public void setFaceId(String faceId) {
- this.faceId = faceId;
- }
- public String getCreateTime() {
- return createTime;
- }
- public void setCreateTime(String createTime) {
- this.createTime = createTime;
- }
- public String getGroupStatus() {
- return groupStatus;
- }
- public void setGroupStatus(String groupStatus) {
- this.groupStatus = groupStatus;
- }
- public String getSuperExpireDate() {
- return superExpireDate;
- }
- public void setSuperExpireDate(String superExpireDate) {
- this.superExpireDate = superExpireDate;
- }
- }
後面是2個工具類,一個是讀取頁面的程式碼
- package com.laozizhu.blog.util;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.InputStreamReader;
- import java.net.ConnectException;
- import java.net.HttpURLConnection;
- import java.net.URL;
- /**
- * 讀取URL的文字
- *
- * @author 老紫竹 www.laozizhu.com
- */
- public class PageService {
- /**
- * 讀取文字。預設使用UTF-8編碼
- *
- * @param page 頁面的URL,比如 http://www.java2000.net
- * @return 讀取到的文字字串
- */
- public static String getPage(String page) {
- return getPage(page, "UTF-8");
- }
- /**
- * 讀取文字
- *
- * @param page 頁面的URL,比如 http://www.java2000.net
- * @param charset 頁面的編碼
- * @return 讀取到的文字字串
- */
- public static String getPage(String page, String charset) {
- String str = null;
- int count = 3;
- do {
- str = _getPage(page, charset, null);
- if (str == null || str.length() == 0) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- } while (str == null && count-- > 0);
- return str;
- }
- public static String getPage(String page, String charset, String cookies) {
- String str = null;
- int count = 3;
- do {
- str = _getPage(page, charset, cookies);
- if (str == null || str.length() == 0) {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- } while (str == null && count-- > 0);
- return str;
- }
- /**
- * 讀取頁面資訊
- *
- * @param page 頁面的URL
- * @param charset 頁面的編碼型別
- * @return 頁面的字串,注意換行已經被預設去掉了,如果需要,請看程式碼的註釋部分
- */
- private static String _getPage(String page, String charset, String cookies) {
- try {
- URL url = new URL(page);
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- if (cookies != null) {
- con.addRequestProperty("Cookie", cookies);
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
- StringBuilder b = new StringBuilder();
- String line;
- while ((line = reader.readLine()) != null) {
- b.append(line);
- b.append("/r/n"); // 預設這裡沒有儲存換行,而是讓所有的字元出現在一行裡面。如果需要,請去掉前面的註釋
- }
- return b.toString();
- } catch (FileNotFoundException ex) {
- System.out.println("NOT FOUND:" + page);
- return null;
- } catch (ConnectException ex) {
- System.out.println("Timeout:" + page);
- return null;
- } catch (Exception ex) {
- ex.printStackTrace();
- return null;
- }
- }
- }
另一個是html轉碼的類
- package com.laozizhu.blog.util;
- import java.util.HashMap;
- /**
- * 替換HTMl裡面的字元 e.g.: < > " å И 水
- *
- * @author 老紫竹 www.laozizhu.com
- */
- public class MyHTMLDecoder {
- public static final HashMap<String, Character> charTable;
- public static String decode(String s) {
- try {
- String t;
- Character ch;
- int tmpPos, i;
- int maxPos = s.length();
- StringBuffer sb = new StringBuffer(maxPos);
- int curPos = 0;
- while (curPos < maxPos) {
- char c = s.charAt(curPos++);
- if (c == '&') {
- tmpPos = curPos;
- if (tmpPos < maxPos) {
- char d = s.charAt(tmpPos++);
- if (d == '#') {
- if (tmpPos < maxPos) {
- d = s.charAt(tmpPos++);
- if ((d == 'x') || (d == 'X')) {
- if (tmpPos < maxPos) {
- d = s.charAt(tmpPos++);
- if (isHexDigit(d)) {
- while (tmpPos < maxPos) {
- d = s.charAt(tmpPos++);
- if (!isHexDigit(d)) {
- if (d == ';') {
- t = s.substring(curPos + 2, tmpPos - 1);
- try {
- i = Integer.parseInt(t, 16);
- if ((i >= 0) && (i < 65536)) {
- c = (char) i;
- curPos = tmpPos;
- }
- } catch (NumberFormatException e) {}
- }
- break;
- }
- }
- }
- }
- } else if (isDigit(d)) {
- while (tmpPos < maxPos) {
- d = s.charAt(tmpPos++);
- if (!isDigit(d)) {
- if (d == ';') {
- t = s.substring(curPos + 1, tmpPos - 1);
- try {
- i = Integer.parseInt(t);
- if ((i >= 0) && (i < 65536)) {
- c = (char) i;
- curPos = tmpPos;
- }
- } catch (NumberFormatException e) {}
- }
- break;
- }
- }
- }
- }
- } else if (isLetter(d)) {