1. 程式人生 > >Java通過QQ群的API讀取群組狀態的程式碼

Java通過QQ群的API讀取群組狀態的程式碼

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

有好多群,懶得隨時更新群的狀態,特別是當前人數這個經常變的東西。這裡根據QQ的一個API程式,讀取狀態,直接看看程式碼吧。

讀取的介面程式和測試程式碼!
  1. package
     com.laozizhu.blog.util;
  2. import java.io.Reader;
  3. import java.io.StringReader;
  4. import java.util.List;
  5. import org.jdom.Document;
  6. import org.jdom.Element;
  7. import org.jdom.input.SAXBuilder;
  8. /**
  9.  * 讀取的介面程式。
  10.  * 
  11.  * @author 老紫竹 www.laozizhu.com
  12.  */
  13. public class QQGroupBaseInfoAPI {
  14.   public static QQGroupBaseInfo get(String id) {
  15.     try {
  16.       String textXml = PageService.getPage(
    "http://group.qq.com/cgi-bin/groupbaseinfo?groupid=" + id, "GBK");
  17.       char c = 0x1d;
  18.       String s = String.valueOf(c);
  19.       textXml = textXml.replaceAll(s, "");
  20.       SAXBuilder builder = new SAXBuilder();
  21.       Document doc = null;
  22.       Reader in = new StringReader(textXml);
  23.       doc = builder.build(in);
  24.       Element root = doc.getRootElement(); // 拿到 date
  25.       List date = root.getChildren(); // 下一級資料
  26.       Element result = (Element) date.get(0); // 結果標誌
  27.       if (!"0".equals(result.getTextTrim())) {
  28.         System.out.println("沒找到" + id);
  29.         return null// 沒找到
  30.       }
  31.       Element item = (Element) date.get(1);
  32.       QQGroupBaseInfo o = new QQGroupBaseInfo();
  33.       o.setLogo(MyHTMLDecoder.decode(item.getChildTextTrim("logo")));
  34.       o.setMemo(MyHTMLDecoder.decode(item.getChildTextTrim("memo")));
  35.       o.setMale(MyHTMLDecoder.decode(item.getChildTextTrim("male")));
  36.       o.setFemale(MyHTMLDecoder.decode(item.getChildTextTrim("female")));
  37.       o.setRank(MyHTMLDecoder.decode(item.getChildTextTrim("rank")));
  38.       o.setGroupId(MyHTMLDecoder.decode(item.getChildTextTrim("groupid")));
  39.       o.setGroupType(MyHTMLDecoder.decode(item.getChildTextTrim("grouptype")));
  40.       o.setGroupOption(MyHTMLDecoder.decode(item.getChildTextTrim("logo")));
  41.       o.setBrief(MyHTMLDecoder.decode(item.getChildTextTrim("brief")));
  42.       o.setGroupClass(MyHTMLDecoder.decode(item.getChildTextTrim("groupclass")));
  43.       o.setCreator(MyHTMLDecoder.decode(item.getChildTextTrim("creator")));
  44.       o.setUin(MyHTMLDecoder.decode(item.getChildTextTrim("uin")));
  45.       o.setFaceId(MyHTMLDecoder.decode(item.getChildTextTrim("faceid")));
  46.       o.setCreateTime(MyHTMLDecoder.decode(item.getChildTextTrim("crttime")));
  47.       o.setGroupStatus(MyHTMLDecoder.decode(item.getChildTextTrim("gpstatus")));
  48.       o.setClassName(MyHTMLDecoder.decode(item.getChildTextTrim("class")));
  49.       o.setFlag(Long.parseLong(item.getChildTextTrim("flag")));
  50.       o.setTotal(Integer.parseInt(item.getChildTextTrim("total")));
  51.       o.setGuestDeny(Integer.parseInt(item.getChildTextTrim("guestdeny")));
  52.       o.setMemberMax(Integer.parseInt(item.getChildTextTrim("membermax")));
  53.       o.setGroupFace(Integer.parseInt(item.getChildTextTrim("groupface")));
  54.       o.setGroupName(MyHTMLDecoder.decode(item.getChildTextTrim("groupname")));
  55.       o.setOpen(Integer.parseInt(item.getChildTextTrim("isopen")) == 1);
  56.       o.setOpen(Integer.parseInt(item.getChildTextTrim("isopen")) == 1);
  57.       o.setCloseExpire(Integer.parseInt(item.getChildTextTrim("iscloseexpire")) == 1);
  58.       o.setSuperExpireDate(MyHTMLDecoder.decode(item.getChildTextTrim("superexpiredate")));
  59.       return o;
  60.     } catch (Exception ex) {
  61.       ex.printStackTrace();
  62.       return null;
  63.     }
  64.   }
  65.   /**
  66.    * @param args
  67.    */
  68.   public static void main(String[] args) {
  69.     QQGroupBaseInfo o = get("56763840");
  70.     System.out.println(o.getTotal());
  71.     System.out.println(o.getMemberMax());
  72.   }
  73. }


讀取的介面資訊
  1. package com.laozizhu.blog.util;
  2. /**
  3.  * QQ的基礎資訊。
  4.  * 
  5.  * @author 老紫竹 www.laozizhu.com
  6.  */
  7. public class QQGroupBaseInfo {
  8.   // 圖示
  9.   private String logo;
  10.   // 公告
  11.   private String memo;
  12.   private String male;
  13.   private String female;
  14.   private String rank;
  15.   // 群號
  16.   private String groupId;
  17.   private String groupType;
  18.   private String groupOption;
  19.   // 簡介
  20.   private String brief;
  21.   private String groupClass;
  22.   private String groupClassEx;
  23.   // 建立人
  24.   private String creator;
  25.   // 建立人的號碼
  26.   private String uin;
  27.   private String faceId;
  28.   private String createTime;
  29.   // 是否為高階群
  30.   private String groupStatus;
  31.   // 分類,比如軟體交流
  32.   private String className;
  33.   private long flag;
  34.   // 當前總人數
  35.   private int total;
  36.   private int guestDeny;
  37.   // 最高人數
  38.   private int memberMax;
  39.   private int groupFace;
  40.   // 群名字
  41.   private String groupName;
  42.   private boolean open;
  43.   private boolean mem;
  44.   private boolean closeExpire;
  45.   // 高階群過期日期
  46.   private String superExpireDate;
  47.   public String getLogo() {
  48.     return logo;
  49.   }
  50.   public void setLogo(String logo) {
  51.     this.logo = logo;
  52.   }
  53.   public String getMemo() {
  54.     return memo;
  55.   }
  56.   public void setMemo(String memo) {
  57.     this.memo = memo;
  58.   }
  59.   public String getGroupId() {
  60.     return groupId;
  61.   }
  62.   public void setGroupId(String groupId) {
  63.     this.groupId = groupId;
  64.   }
  65.   public String getGroupType() {
  66.     return groupType;
  67.   }
  68.   public void setGroupType(String groupType) {
  69.     this.groupType = groupType;
  70.   }
  71.   public String getGroupOption() {
  72.     return groupOption;
  73.   }
  74.   public void setGroupOption(String groupOption) {
  75.     this.groupOption = groupOption;
  76.   }
  77.   public String getGroupClass() {
  78.     return groupClass;
  79.   }
  80.   public void setGroupClass(String groupClass) {
  81.     this.groupClass = groupClass;
  82.   }
  83.   public String getGroupClassEx() {
  84.     return groupClassEx;
  85.   }
  86.   public void setGroupClassEx(String groupClassEx) {
  87.     this.groupClassEx = groupClassEx;
  88.   }
  89.   public String getBrief() {
  90.     return brief;
  91.   }
  92.   public void setBrief(String brief) {
  93.     this.brief = brief;
  94.   }
  95.   public String getCreator() {
  96.     return creator;
  97.   }
  98.   public void setCreator(String creator) {
  99.     this.creator = creator;
  100.   }
  101.   public String getUin() {
  102.     return uin;
  103.   }
  104.   public void setUin(String uin) {
  105.     this.uin = uin;
  106.   }
  107.   public String getClassName() {
  108.     return className;
  109.   }
  110.   public void setClassName(String className) {
  111.     this.className = className;
  112.   }
  113.   public long getFlag() {
  114.     return flag;
  115.   }
  116.   public void setFlag(long flag) {
  117.     this.flag = flag;
  118.   }
  119.   public int getTotal() {
  120.     return total;
  121.   }
  122.   public void setTotal(int total) {
  123.     this.total = total;
  124.   }
  125.   public int getGuestDeny() {
  126.     return guestDeny;
  127.   }
  128.   public void setGuestDeny(int guestDeny) {
  129.     this.guestDeny = guestDeny;
  130.   }
  131.   public int getMemberMax() {
  132.     return memberMax;
  133.   }
  134.   public void setMemberMax(int memberMax) {
  135.     this.memberMax = memberMax;
  136.   }
  137.   public int getGroupFace() {
  138.     return groupFace;
  139.   }
  140.   public void setGroupFace(int groupFace) {
  141.     this.groupFace = groupFace;
  142.   }
  143.   public String getGroupName() {
  144.     return groupName;
  145.   }
  146.   public void setGroupName(String groupName) {
  147.     this.groupName = groupName;
  148.   }
  149.   public boolean isOpen() {
  150.     return open;
  151.   }
  152.   public void setOpen(boolean open) {
  153.     this.open = open;
  154.   }
  155.   public boolean isMem() {
  156.     return mem;
  157.   }
  158.   public void setMem(boolean mem) {
  159.     this.mem = mem;
  160.   }
  161.   public boolean isCloseExpire() {
  162.     return closeExpire;
  163.   }
  164.   public void setCloseExpire(boolean closeExpire) {
  165.     this.closeExpire = closeExpire;
  166.   }
  167.   public String getMale() {
  168.     return male;
  169.   }
  170.   public void setMale(String male) {
  171.     this.male = male;
  172.   }
  173.   public String getFemale() {
  174.     return female;
  175.   }
  176.   public void setFemale(String female) {
  177.     this.female = female;
  178.   }
  179.   public String getRank() {
  180.     return rank;
  181.   }
  182.   public void setRank(String rank) {
  183.     this.rank = rank;
  184.   }
  185.   public String getFaceId() {
  186.     return faceId;
  187.   }
  188.   public void setFaceId(String faceId) {
  189.     this.faceId = faceId;
  190.   }
  191.   public String getCreateTime() {
  192.     return createTime;
  193.   }
  194.   public void setCreateTime(String createTime) {
  195.     this.createTime = createTime;
  196.   }
  197.   public String getGroupStatus() {
  198.     return groupStatus;
  199.   }
  200.   public void setGroupStatus(String groupStatus) {
  201.     this.groupStatus = groupStatus;
  202.   }
  203.   public String getSuperExpireDate() {
  204.     return superExpireDate;
  205.   }
  206.   public void setSuperExpireDate(String superExpireDate) {
  207.     this.superExpireDate = superExpireDate;
  208.   }
  209. }


後面是2個工具類,一個是讀取頁面的程式碼
  1. package com.laozizhu.blog.util;
  2. import java.io.BufferedReader;
  3. import java.io.FileNotFoundException;
  4. import java.io.InputStreamReader;
  5. import java.net.ConnectException;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. /**
  9.  * 讀取URL的文字
  10.  * 
  11.  * @author 老紫竹 www.laozizhu.com
  12.  */
  13. public class PageService {
  14.   /**
  15.    * 讀取文字。預設使用UTF-8編碼
  16.    * 
  17.    * @param page 頁面的URL,比如 http://www.java2000.net
  18.    * @return 讀取到的文字字串
  19.    */
  20.   public static String getPage(String page) {
  21.     return getPage(page, "UTF-8");
  22.   }
  23.   /**
  24.    * 讀取文字
  25.    * 
  26.    * @param page 頁面的URL,比如 http://www.java2000.net
  27.    * @param charset 頁面的編碼
  28.    * @return 讀取到的文字字串
  29.    */
  30.   public static String getPage(String page, String charset) {
  31.     String str = null;
  32.     int count = 3;
  33.     do {
  34.       str = _getPage(page, charset, null);
  35.       if (str == null || str.length() == 0) {
  36.         try {
  37.           Thread.sleep(1000);
  38.         } catch (InterruptedException e) {
  39.           e.printStackTrace();
  40.         }
  41.       }
  42.     } while (str == null && count-- > 0);
  43.     return str;
  44.   }
  45.   public static String getPage(String page, String charset, String cookies) {
  46.     String str = null;
  47.     int count = 3;
  48.     do {
  49.       str = _getPage(page, charset, cookies);
  50.       if (str == null || str.length() == 0) {
  51.         try {
  52.           Thread.sleep(1000);
  53.         } catch (InterruptedException e) {
  54.           e.printStackTrace();
  55.         }
  56.       }
  57.     } while (str == null && count-- > 0);
  58.     return str;
  59.   }
  60.   /**
  61.    * 讀取頁面資訊
  62.    * 
  63.    * @param page 頁面的URL
  64.    * @param charset 頁面的編碼型別
  65.    * @return 頁面的字串,注意換行已經被預設去掉了,如果需要,請看程式碼的註釋部分
  66.    */
  67.   private static String _getPage(String page, String charset, String cookies) {
  68.     try {
  69.       URL url = new URL(page);
  70.       HttpURLConnection con = (HttpURLConnection) url.openConnection();
  71.       if (cookies != null) {
  72.         con.addRequestProperty("Cookie", cookies);
  73.       }
  74.       BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
  75.       StringBuilder b = new StringBuilder();
  76.       String line;
  77.       while ((line = reader.readLine()) != null) {
  78.         b.append(line);
  79.         b.append("/r/n"); // 預設這裡沒有儲存換行,而是讓所有的字元出現在一行裡面。如果需要,請去掉前面的註釋
  80.       }
  81.       return b.toString();
  82.     } catch (FileNotFoundException ex) {
  83.       System.out.println("NOT FOUND:" + page);
  84.       return null;
  85.     } catch (ConnectException ex) {
  86.       System.out.println("Timeout:" + page);
  87.       return null;
  88.     } catch (Exception ex) {
  89.       ex.printStackTrace();
  90.       return null;
  91.     }
  92.   }
  93. }


另一個是html轉碼的類
  1. package com.laozizhu.blog.util;
  2. import java.util.HashMap;
  3. /**
  4.  * 替換HTMl裡面的字元 e.g.: < > " å И 水
  5.  * 
  6.  *  @author 老紫竹 www.laozizhu.com
  7.  */
  8. public class MyHTMLDecoder {
  9.   public static final HashMap<String, Character> charTable;
  10.   public static String decode(String s) {
  11.     try {
  12.       String t;
  13.       Character ch;
  14.       int tmpPos, i;
  15.       int maxPos = s.length();
  16.       StringBuffer sb = new StringBuffer(maxPos);
  17.       int curPos = 0;
  18.       while (curPos < maxPos) {
  19.         char c = s.charAt(curPos++);
  20.         if (c == '&') {
  21.           tmpPos = curPos;
  22.           if (tmpPos < maxPos) {
  23.             char d = s.charAt(tmpPos++);
  24.             if (d == '#') {
  25.               if (tmpPos < maxPos) {
  26.                 d = s.charAt(tmpPos++);
  27.                 if ((d == 'x') || (d == 'X')) {
  28.                   if (tmpPos < maxPos) {
  29.                     d = s.charAt(tmpPos++);
  30.                     if (isHexDigit(d)) {
  31.                       while (tmpPos < maxPos) {
  32.                         d = s.charAt(tmpPos++);
  33.                         if (!isHexDigit(d)) {
  34.                           if (d == ';') {
  35.                             t = s.substring(curPos + 2, tmpPos - 1);
  36.                             try {
  37.                               i = Integer.parseInt(t, 16);
  38.                               if ((i >= 0) && (i < 65536)) {
  39.                                 c = (char) i;
  40.                                 curPos = tmpPos;
  41.                               }
  42.                             } catch (NumberFormatException e) {}
  43.                           }
  44.                           break;
  45.                         }
  46.                       }
  47.                     }
  48.                   }
  49.                 } else if (isDigit(d)) {
  50.                   while (tmpPos < maxPos) {
  51.                     d = s.charAt(tmpPos++);
  52.                     if (!isDigit(d)) {
  53.                       if (d == ';') {
  54.                         t = s.substring(curPos + 1, tmpPos - 1);
  55.                         try {
  56.                           i = Integer.parseInt(t);
  57.                           if ((i >= 0) && (i < 65536)) {
  58.                             c = (char) i;
  59.                             curPos = tmpPos;
  60.                           }
  61.                         } catch (NumberFormatException e) {}
  62.                       }
  63.                       break;
  64.                     }
  65.                   }
  66.                 }
  67.               }
  68.             } else if (isLetter(d)) {
  69.