分享下之前调用过的足球杯赛联赛资料代码
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
/**
* @API: 5.杯赛联赛资料
*/
public class FootballLeagueInfo {
public static void main(String[] args) {
try {
String content = getContent();
JAXBContext jaxbContext = JAXBContext.newInstance(LeagueList.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
LeagueList leagueList = (LeagueList) unmarshaller.unmarshal(new ByteArrayInputStream(content.getBytes()));
leagueList.getLeagueList().forEach(item -> System.out.println(item));
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* 获取API返回内容
*
* Note: 这里为了方便测试我使用了一份本地文件,使用时应替换为真实接口返回内容
*/
private static String getContent() {
try {
StringBuilder builder = new StringBuilder();
List<String> lines = Files.readAllLines(Paths.get("./src/main/resources/FootballLeagueInfo.xml"), StandardCharsets.UTF_8);
lines.forEach(line -> builder.append(line));
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
return "";
}
}
@XmlRootElement(name = "list")
public static class LeagueList {
@XmlElement(name = "match")
private List<League> leagueList;
public List<League> getLeagueList() {
return leagueList;
}
}
@XmlRootElement
public static class League {
@XmlElement
private String id;
@XmlElement
private String gb_short;
@XmlElement
private String big_short;
@XmlElement
private String en_short;
@XmlElement
private String gb;
@XmlElement
private String big;
@XmlElement
private String en;
@XmlElement
private int type;
@XmlElement
private String subSclass;
@XmlElement
private int sum_round;
@XmlElement
private int curr_round;
@XmlElement
private String Curr_matchSeason;
@XmlElement
private String countryID;
@XmlElement
private String country;
@XmlElement
private String areaID;
@XmlElement
private String countryEn;
@XmlElement
private String logo;
@XmlElement
private String countryLogo;
@Override
public String toString() {
return "League{" +
"id='" + id + '\'' +
", gb_short='" + gb_short + '\'' +
", big_short='" + big_short + '\'' +
", en_short='" + en_short + '\'' +
", gb='" + gb + '\'' +
", big='" + big + '\'' +
", en='" + en + '\'' +
", type=" + type +
", subSclass='" + subSclass + '\'' +
", sum_round=" + sum_round +
", curr_round=" + curr_round +
", Curr_matchSeason='" + Curr_matchSeason + '\'' +
", countryID='" + countryID + '\'' +
", country='" + country + '\'' +
", areaID='" + areaID + '\'' +
", countryEn='" + countryEn + '\'' +
", logo='" + logo + '\'' +
", countryLogo='" + countryLogo + '\'' +
'}';
}
public String getId() {
return id;
}
public String getGb_short() {
return gb_short;
}
public String getBig_short() {
return big_short;
}
public String getEn_short() {
return en_short;
}
public String getGb() {
return gb;
}
public String getBig() {
return big;
}
public String getEn() {
return en;
}
public int getType() {
return type;
}
public String getSubSclass() {
return subSclass;
}
public int getSum_round() {
return sum_round;
}
public int getCurr_round() {
return curr_round;
}
public String getCurr_matchSeason() {
return Curr_matchSeason;
}
public String getCountryID() {
return countryID;
}
public String getCountry() {
return country;
}
public String getAreaID() {
return areaID;
}
public String getCountryEn() {
return countryEn;
}
public String getLogo() {
return logo;
}
public String getCountryLogo() {
return countryLogo;
}
}
}
返回数据如下:
League{id='1', gb_short='爱超', big_short='愛超', en_short='IRE PR', gb='爱尔兰超级联赛', big='愛爾蘭超級聯賽', en='Ireland Premier Division', type=1, subSclass='联赛', sum_round=36, curr_round=20, Curr_matchSeason='2019', countryID='16', country='爱尔兰', areaID='1', countryEn='Ireland', logo='http://zq.win007.com/Image/league_match/images/20181129190115.png', countryLogo='http://zq.win007.com/Image/info/images/200712304261564347.gif'}
League{id='2', gb_short='阿甲', big_short='阿甲', en_short='ARG D1', gb='阿根廷甲级联赛', big='阿根廷甲級聯賽', en='Argentine Division 1', type=1, subSclass='联赛', sum_round=25, curr_round=25, Curr_matchSeason='2018-2019', countryID='38', country='阿根廷', areaID='2', countryEn='Argentina', logo='http://zq.win007.com/Image/league_match/images/20130917105542.jpg', countryLogo='http://zq.win007.com/Image/info/images/20130121182844.jpg'}
League{id='3', gb_short='奥甲', big_short='奧甲', en_short='AUT D1', gb='奥地利甲组联赛', big='奧地利甲組聯賽', en='Austrian Bundesliga', type=1, subSclass='联赛', sum_round=22, curr_round=1, Curr_matchSeason='2019-2020', countryID='14', country='奥地利', areaID='1', countryEn='Austria', logo='http://zq.win007.com/Image/league_match/images/20140122115024.jpg', countryLogo='http://zq.win007.com/Image/info/images/20130801165112.jpg'}
League{id='4', gb_short='巴西甲', big_short='巴西甲', en_short='BRA D1', gb='巴西甲组联赛', big='巴西甲組聯賽', en='Brazil Serie A', type=1, subSclass='null', sum_round=38, curr_round=10, Curr_matchSeason='2019', countryID='39', country='巴西', areaID='2', countryEn='Brazil', logo='http://zq.win007.com/Image/league_match/images/20170507175810.png', countryLogo='http://zq.win007.com/Image/info/images/20130121200051.jpg'}
League{id='5', gb_short='比甲', big_short='比甲', en_short='BEL D1', gb='比利时甲组联赛', big='比利時甲組聯賽', en='Belgian Pro League', type=1, subSclass='联赛', sum_round=30, curr_round=1, Curr_matchSeason='2019-2020', countryID='9', country='比利时', areaID='1', countryEn='Belgium', logo='http://zq.win007.com/Image/league_match/images/20181204180011.png', countryLogo='http://zq.win007.com/Image/info/images/20130121180759.jpg'}
League{id='6', gb_short='波兰超', big_short='波蘭超', en_short='POL PR', gb='波兰超级联赛', big='波蘭超级聯賽', en='Poland Ekstraklasa', type=1, subSclass='联赛', sum_round=30, curr_round=1, Curr_matchSeason='2019-2020', countryID='19', country='波兰', areaID='1', countryEn='Poland', logo='http://zq.win007.com/Image/league_match/images/20160619101249.jpg', countryLogo='http://zq.win007.com/Image/info/images/20130121175428.jpg'}