分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
有好多群,懒得随时更新群的状态,特别是当前人数这个经常变的东西。这里根据QQ的一个API程序,读取状态,直接看看代码吧。
读取的接口程序和测试代码!
- package com.laozizhu.blog.util;
- 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("http://group.qq.com/cgi-bin/groupbaseinfo?groupid=" + id, "GBK");
- 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)) {
- while (tmpPos < maxPos) {
- d = s.charAt(tmpPos++);
- if (!isLetterOrDigit(d)) {
- if (d == ';') {
- t = s.substring(curPos, tmpPos - 1);
- ch = (Character) charTable.get(t);
- if (ch != null) {
- c = ch.charValue();
- curPos = tmpPos;
- }
- }
- break;
- }
- }
- }
- }
- }
- sb.append(c);
- }
- return sb.toString();
- } catch (Exception ex) {
- return s;
- }
- }
- private static boolean isLetterOrDigit(char c) {
- return isLetter(c) || isDigit(c);
- }
- private static boolean isHexDigit(char c) {
- return isHexLetter(c) || isDigit(c);
- }
- private static boolean isLetter(char c) {
- return ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'));
- }
- private static boolean isHexLetter(char c) {
- return ((c >= 'a') && (c <= 'f')) || ((c >= 'A') && (c <= 'F'));
- }
- private static boolean isDigit(char c) {
- return (c >= '0') && (c <= '9');
- }
- public static String compact(String s) {
- int maxPos = s.length();
- StringBuffer sb = new StringBuffer(maxPos);
- int curPos = 0;
- while (curPos < maxPos) {
- char c = s.charAt(curPos++);
- if (isWhitespace(c)) {
- while ((curPos < maxPos) && isWhitespace(s.charAt(curPos))) {
- curPos++;
- }
- c = '/u0020';
- }
- sb.append(c);
- }
- return sb.toString();
- }
- // HTML is very particular about what constitutes white space.
- public static boolean isWhitespace(char ch) {
- return (ch == '/u0020') || (ch == '/r') || (ch == '/n') || (ch == '/u0009') || (ch == '/u000c') || (ch == '/u200b');
- }
- static {
- charTable = new HashMap<String, Character>();
- charTable.put("quot", new Character((char) 34));
- charTable.put("amp", new Character((char) 38));
- charTable.put("apos", new Character((char) 39));
- charTable.put("lt", new Character((char) 60));
- charTable.put("gt", new Character((char) 62));
- charTable.put("nbsp", new Character((char) 160));
- charTable.put("iexcl", new Character((char) 161));
- charTable.put("cent", new Character((char) 162));
- charTable.put("pound", new Character((char) 163));
- charTable.put("curren", new Character((char) 164));
- charTable.put("yen", new Character((char) 165));
- charTable.put("brvbar", new Character((char) 166));
- charTable.put("sect", new Character((char) 167));
- charTable.put("uml", new Character((char) 168));
- charTable.put("copy", new Character((char) 169));
- charTable.put("ordf", new Character((char) 170));
- charTable.put("laquo", new Character((char) 171));
- charTable.put("not", new Character((char) 172));
- charTable.put("shy", new Character((char) 173));
- charTable.put("reg", new Character((char) 174));
- charTable.put("macr", new Character((char) 175));
- charTable.put("deg", new Character((char) 176));
- charTable.put("plusmn", new Character((char) 177));
- charTable.put("sup2", new Character((char) 178));
- charTable.put("sup3", new Character((char) 179));
- charTable.put("acute", new Character((char) 180));
- charTable.put("micro", new Character((char) 181));
- charTable.put("para", new Character((char) 182));
- charTable.put("middot", new Character((char) 183));
- charTable.put("cedil", new Character((char) 184));
- charTable.put("sup1", new Character((char) 185));
- charTable.put("ordm", new Character((char) 186));
- charTable.put("raquo", new Character((char) 187));
- charTable.put("frac14", new Character((char) 188));
- charTable.put("frac12", new Character((char) 189));
- charTable.put("frac34", new Character((char) 190));
- charTable.put("iquest", new Character((char) 191));
- charTable.put("Agrave", new Character((char) 192));
- charTable.put("Aacute", new Character((char) 193));
- charTable.put("Acirc", new Character((char) 194));
- charTable.put("Atilde", new Character((char) 195));
- charTable.put("Auml", new Character((char) 196));
- charTable.put("Aring", new Character((char) 197));
- charTable.put("AElig", new Character((char) 198));
- charTable.put("Ccedil", new Character((char) 199));
- charTable.put("Egrave", new Character((char) 200));
- charTable.put("Eacute", new Character((char) 201));
- charTable.put("Ecirc", new Character((char) 202));
- charTable.put("Euml", new Character((char) 203));
- charTable.put("Igrave", new Character((char) 204));
- charTable.put("Iacute", new Character((char) 205));
- charTable.put("Icirc", new Character((char) 206));
- charTable.put("Iuml", new Character((char) 207));
- charTable.put("ETH", new Character((char) 208));
- charTable.put("Ntilde", new Character((char) 209));
- charTable.put("Ograve", new Character((char) 210));
- charTable.put("Oacute", new Character((char) 211));
- charTable.put("Ocirc", new Character((char) 212));
- charTable.put("Otilde", new Character((char) 213));
- charTable.put("Ouml", new Character((char) 214));
- charTable.put("times", new Character((char) 215));
- charTable.put("Oslash", new Character((char) 216));
- charTable.put("Ugrave", new Character((char) 217));
- charTable.put("Uacute", new Character((char) 218));
- charTable.put("Ucirc", new Character((char) 219));
- charTable.put("Uuml", new Character((char) 220));
- charTable.put("Yacute", new Character((char) 221));
- charTable.put("THORN", new Character((char) 222));
- charTable.put("szlig", new Character((char) 223));
- charTable.put("agrave", new Character((char) 224));
- charTable.put("aacute", new Character((char) 225));
- charTable.put("acirc", new Character((char) 226));
- charTable.put("atilde", new Character((char) 227));
- charTable.put("auml", new Character((char) 228));
- charTable.put("aring", new Character((char) 229));
- charTable.put("aelig", new Character((char) 230));
- charTable.put("ccedil", new Character((char) 231));
- charTable.put("egrave", new Character((char) 232));
- charTable.put("eacute", new Character((char) 233));
- charTable.put("ecirc", new Character((char) 234));
- charTable.put("euml", new Character((char) 235));
- charTable.put("igrave", new Character((char) 236));
- charTable.put("iacute", new Character((char) 237));
- charTable.put("icirc", new Character((char) 238));
- charTable.put("iuml", new Character((char) 239));
- charTable.put("eth", new Character((char) 240));
- charTable.put("ntilde", new Character((char) 241));
- charTable.put("ograve", new Character((char) 242));
- charTable.put("oacute", new Character((char) 243));
- charTable.put("ocirc", new Character((char) 244));
- charTable.put("otilde", new Character((char) 245));
- charTable.put("ouml", new Character((char) 246));
- charTable.put("divide", new Character((char) 247));
- charTable.put("oslash", new Character((char) 248));
- charTable.put("ugrave", new Character((char) 249));
- charTable.put("uacute", new Character((char) 250));
- charTable.put("ucirc", new Character((char) 251));
- charTable.put("uuml", new Character((char) 252));
- charTable.put("yacute", new Character((char) 253));
- charTable.put("thorn", new Character((char) 254));
- charTable.put("yuml", new Character((char) 255));
-
给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow