先来张效果图
首先新建两个实体类
//玩家的信息public class Player { private String id;// 用户名 private String tier;//大段位 private String rank;// 小段位 private String league_points;// 胜点 private String rank_games;// 排位场数 private String rank_winRate;// 排位胜率 private String general_games;// 匹配场数 private String general_winRate;// 匹配胜率 private String loginTime;// 最后登录时间 private String fighting;// 战斗力 private int latelyGameRate;//最近几场游戏胜率 private String assess;//评价 public String getAssess() { return assess; } public void setAssess(String assess) { this.assess = assess; } public String getTier() { return tier; } public void setTier(String tier) { this.tier = tier; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getRank() { return rank; } public void setRank(String rank) { this.rank = rank; } public String getLeague_points() { return league_points; } public void setLeague_points(String league_points) { this.league_points = league_points; } public String getRank_games() { return rank_games; } public void setRank_games(String rank_games) { this.rank_games = rank_games; } public String getRank_winRate() { return rank_winRate; } public void setRank_winRate(String rank_winRate) { this.rank_winRate = rank_winRate; } public String getGeneral_games() { return general_games; } public void setGeneral_games(String general_games) { this.general_games = general_games; } public String getGeneral_winRate() { return general_winRate; } public void setGeneral_winRate(String general_winRate) { this.general_winRate = general_winRate; } public String getLoginTime() { return loginTime; } public void setLoginTime(String loginTime) { this.loginTime = loginTime; } public String getFighting() { return fighting; } public void setFighting(String fighting) { this.fighting = fighting; } public int getLatelyGameRate() { return latelyGameRate; } public void setLatelyGameRate(int latelyGameRate) { this.latelyGameRate = latelyGameRate; }}
import net.sf.json.JSONArray;//英雄信息public class Hero { private String championName; private String championNameCN; private String winRate;// 胜率 private String matchStat;// 场数 private JSONArray averageKDA;// 场均 杀/死/助 private String averageKDARating;// 场均KDA private JSONArray averageDamage;// 场均输出 private JSONArray averageEarn; private String averageMinionsKilled; private String totalMVP;// MVP次数 private String totalHope; @Override public String toString() { return "Hero [championNameCN=" + championNameCN + ", winRate=" + winRate + ", matchStat=" + matchStat + ", averageKDA=" + averageKDA + ", averageKDARating=" + averageKDARating + ", totalMVP=" + totalMVP + "]"; } private JSONArray championPoints; public String getChampionName() { return championName; } public void setChampionName(String championName) { this.championName = championName; } public String getChampionNameCN() { return championNameCN; } public void setChampionNameCN(String championNameCN) { this.championNameCN = championNameCN; } public String getWinRate() { return winRate; } public void setWinRate(String winRate) { this.winRate = winRate; } public String getMatchStat() { return matchStat; } public void setMatchStat(String matchStat) { this.matchStat = matchStat; } public JSONArray getAverageKDA() { return averageKDA; } public void setAverageKDA(JSONArray averageKDA) { this.averageKDA = averageKDA; } public String getAverageKDARating() { return averageKDARating; } public void setAverageKDARating(String averageKDARating) { this.averageKDARating = averageKDARating; } public JSONArray getAverageDamage() { return averageDamage; } public void setAverageDamage(JSONArray averageDamage) { this.averageDamage = averageDamage; } public JSONArray getAverageEarn() { return averageEarn; } public void setAverageEarn(JSONArray averageEarn) { this.averageEarn = averageEarn; } public String getAverageMinionsKilled() { return averageMinionsKilled; } public void setAverageMinionsKilled(String averageMinionsKilled) { this.averageMinionsKilled = averageMinionsKilled; } public String getTotalMVP() { return totalMVP; } public void setTotalMVP(String totalMVP) { this.totalMVP = totalMVP; } public String getTotalHope() { return totalHope; } public void setTotalHope(String totalHope) { this.totalHope = totalHope; } public JSONArray getChampionPoints() { return championPoints; } public void setChampionPoints(JSONArray championPoints) { this.championPoints = championPoints; }}
再建一个枚举,用于保存和查询服务器名字
import java.util.HashMap;import java.util.Map;// 定义枚举类public enum ENUMServerData { server1("电信一", "艾欧尼亚"), server2("网通一", "比尔吉沃特"), server3("电信二", "祖安"), server4("电信三", "诺克萨斯"), server5( "网通二", "德玛西亚"), server6("电信四", "班德尔城"), server7("电信五", "皮尔特沃夫"), server8("电信六", "战争学院"), server9( "网通三", "弗雷尔卓德"), server10("电信七", "巨神峰"), server11("电信八", "雷瑟守备"), server12("网通四", "无畏先锋"), server13("电信九", "裁决之地"), server14("电信十", "黑色玫瑰"), server15("电信十一", "暗影岛"), server16( "电信十二", "钢铁烈阳"), server17("网通五", "恕瑞玛"), server18("电信十三", "均衡教派"), server19("电信十四", "水晶之痕"), server20("电信十五", "影流"), server21("电信十六", "守望之海"), server22("网通六", "扭曲丛林"), server23( "电信十七", "征服之海"), server24("电信十八", "卡拉曼达"), server25("电信十九", "皮城警备"), server26("网通七", "巨龙之巢"); private String serverNum; private String serverName; private ENUMServerData(String a, String b) { this.serverNum = a; this.serverName = b; } public String getServerName() { return serverName; } public String getServerNum() { return serverNum; } //根据残缺的大区名,智能从枚举中取完整大区名 //如输入黑色,即可识别为黑色玫瑰 public static MapGetServer(String s) { Map map = null; ENUMServerData[] sd = ENUMServerData.values(); for (ENUMServerData server : sd) { if ((server.getServerNum() + server.getServerName()).indexOf(s) != -1) { map = new HashMap (); map.put("serverName", server.getServerName()); map.put("serverNum", server.getServerNum()); break; } } return map; }}
查询
import java.net.URLEncoder;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import net.sf.json.JSONArray;import net.sf.json.JSONObject;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.select.Elements;import com.yemao.pojo.Hero;import com.yemao.pojo.Player;import util.WebUtil;public class lolService { /** * 查询 * * @param server * 大区 * @param name * 玩家ID * @return * @throws InterruptedException * @throws ExecutionException */ public static Mapquery(String server, String name) throws InterruptedException, ExecutionException { // 从枚举中查询对应的名称 Map smap = ENUMServerData.GetServer(server); String serverName = smap.get("serverName").toString(); String serverNum = smap.get("serverNum").toString(); // 返回的Map Map map = new HashMap (); // 建立一个线程池 ExecutorService pool = Executors.newFixedThreadPool(2); Callable
测试
public static void main(String[] args) { try { String name = "玩家ID"; //枚举那里做了智能判断,输入大区名前几个字也可,如黑色自动识别为黑色玫瑰 String server = "大区"; Mapmap = lolService.query(server, name); System.out.println(map.get("heroData")); System.out.println(map.get(userData")); } catch (Exception e) { // TODO: handle exception }}