分享使用接口调用的示例代码,篮球数据【球队资料】接口
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 BasketballTeamInfo {
public static void main(String[] args) {
try {
String content = getContent();
JAXBContext jaxbContext = JAXBContext.newInstance(TeamList.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
TeamList list = (TeamList) unmarshaller.unmarshal(new ByteArrayInputStream(content.getBytes()));
list.getTeamList().forEach(System.out::println);
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* 获取API返回内容
*
* Note: 这里为了方便测试我使用了一份本地文件,使用时应替换为真实接口返回内容
*/
private static String getContent() {
try {
StringBuilder builder = new StringBuilder();
List lines = Files.readAllLines(Paths.get("./src/main/resources/BasketballTeamInfo.xml"), StandardCharsets.UTF_8);
lines.forEach(builder::append);
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
return "";
}
}
@XmlRootElement(name = "list")
public static class TeamList {
@XmlElement(name = "i")
private List teamList;
public List getTeamList() {
return teamList;
}
}
public static class Team {
@XmlElement(name = "id")
private String id;
@XmlElement(name = "lsID")
private String leagueId;
@XmlElement(name = "short")
private String nameShort;
@XmlElement(name = "gb")
private String nameGb;
@XmlElement(name = "big5")
private String nameBig;
@XmlElement(name = "en")
private String nameEn;
@XmlElement(name = "logo")
private String logo;
@XmlElement(name = "LocationID")
private String locationId;
@XmlElement(name = "MatchAddrID")
private String areaId;
@XmlElement(name = "url")
private String website;
@XmlElement(name = "City")
private String city;
@XmlElement(name = "Gymnasium")
private String gym;
@XmlElement(name = "Capacity")
private int capacity;
@XmlElement(name = "JoinYear")
private int joinYear;
@XmlElement(name = "FirstTime")
private int championCount;
@XmlElement(name = "Drillmaster")
private String drillmaster;
@Override
public String toString() {
return "Team{" +
"id='" + id + '\'' +
", nameShort='" + nameShort + '\'' +
", nameGb='" + nameGb + '\'' +
", nameBig='" + nameBig + '\'' +
", nameEn='" + nameEn + '\'' +
", logo='" + logo + '\'' +
", locationId='" + locationId + '\'' +
", areaId='" + areaId + '\'' +
", website='" + website + '\'' +
", city='" + city + '\'' +
", gym='" + gym + '\'' +
", capacity=" + capacity +
", joinYear=" + joinYear +
", championCount=" + championCount +
", drillmaster='" + drillmaster + '\'' +
'}';
}
}
}
API 返回数据如下(部分):
Team{id='1', nameShort='湖人', nameGb='洛杉矶湖人', nameBig='洛杉磯湖人', nameEn='Los Angeles Lakers', logo='/files/team/20120112163535.jpg', locationId='2', areaId='4', website='http://www.nba.com/lakers/', city='加利福尼亚州 洛杉矶市', gym='斯台普斯球馆', capacity=19282, joinYear=0, championCount=16, drillmaster='卢克-沃顿'}
Team{id='2', nameShort='凯尔特人', nameGb='波士顿凯尔特人', nameBig='波士頓塞爾特人', nameEn='Boston Celtics', logo='/files/team/20120112150854.jpg', locationId='1', areaId='1', website='http://www.nba.com/celtics/', city='马萨诸塞洲 波士顿市', gym='北岸花园球馆', capacity=18624, joinYear=1946, championCount=17, drillmaster='布拉德-史蒂文斯'}
Team{id='3', nameShort='热火', nameGb='迈阿密热火', nameBig='邁亞密熱火', nameEn='Miami Heat', logo='/files/team/20120112154051.jpg', locationId='1', areaId='3', website='http://www.nba.com/heat/', city='佛罗里达州 迈阿密市', gym='美国航空球场', capacity=20000, joinYear=1988, championCount=1, drillmaster='斯波尔斯特拉'}
Team{id='4', nameShort='篮网', nameGb='布鲁克林篮网', nameBig='布魯克林籃網', nameEn='Brooklyn Nets', logo='/files/team/20120821112729.jpg', locationId='1', areaId='1', website='http://www.nba.com/nets/', city='纽约州布鲁克林', gym='巴克莱中心球场', capacity=18103, joinYear=0, championCount=1, drillmaster='肯尼-阿特金森'}
Team{id='5', nameShort='尼克斯', nameGb='纽约尼克斯', nameBig='紐約人', nameEn='New York Knicks', logo='/files/team/20120112165918.jpg', locationId='1', areaId='1', website='http://www.nba.com/knicks/', city='纽约州 纽约市', gym='麦迪逊公园球场', capacity=19763, joinYear=0, championCount=2, drillmaster='大卫·菲兹戴尔'}
Team{id='6', nameShort='魔术', nameGb='奥兰多魔术', nameBig='奧蘭多魔術', nameEn='Orlando Magic', logo='/files/team/20120112171211.jpg', locationId='1', areaId='3', website='http://www.nba.com/magic/', city='佛罗里达州 奥兰多市', gym='安利球馆', capacity=17519, joinYear=0, championCount=0, drillmaster=''}
Team{id='7', nameShort='76人', nameGb='费城76人', nameBig='費城七十六人', nameEn='Philadelphia 76ers', logo='/files/team/20180117152851.png', locationId='1', areaId='1', website='http://www.nba.com/sixers/', city='宾夕法尼亚州 费城市', gym='富国银行中心球馆', capacity=0, joinYear=0, championCount=3, drillmaster='布雷特-布朗'}
Team{id='8', nameShort='奇才', nameGb='华盛顿奇才', nameBig='華盛頓巫師', nameEn='Washington Wizards', logo='/files/team/20180117153230.png', locationId='1', areaId='3', website='http://www.nba.com/wizards/', city='华盛顿特区 华盛顿', gym='维尔佐中心球馆', capacity=0, joinYear=0, championCount=1, drillmaster='斯科特-布鲁克斯'}
Team{id='9', nameShort='活塞', nameGb='底特律活塞', nameBig='底特律活塞', nameEn='Detroit Pistons', logo='/files/team/20180117153734.png', locationId='1', areaId='2', website='http://www.nba.com/pistons/', city='密歇根州 底特律市', gym='小凯撒球馆', capacity=20491, joinYear=0, championCount=3, drillmaster='范甘迪'}