分享使用接口调用的示例代码,Dota2的【联赛列表】接口。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
/**
* @API: 联赛列表
*/
public class DotaLeague {
public static void main(String[] args) {
try {
String content = getContent();
Respond rsp = JSON.parseObject(content, Respond.class);
System.out.println(rsp.code);
System.out.println(rsp.message);
rsp.getLeagueList().forEach(System.out::println);
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* 获取API返回内容
* <p>
* Note: 这里为了方便测试我使用了一份本地文件,使用时应替换为真实接口返回内容
*/
private static String getContent() {
try {
StringBuilder builder = new StringBuilder();
List<String> lines = Files.readAllLines(Paths.get("./src/main/resources/DotaLeague.json"), StandardCharsets.UTF_8);
lines.forEach(builder::append);
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
return "";
}
}
public static class Respond {
@JSONField(name = "code")
private int code;
@JSONField(name = "message")
private String message;
@JSONField(name = "data")
private List<League> leagueList;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<League> getLeagueList() {
return leagueList;
}
public void setLeagueList(List<League> leagueList) {
this.leagueList = leagueList;
}
}
public static class League {
@JSONField(name = "league_id")
private int leagueId;
@JSONField(name = "name")
private String name;
@JSONField(name = "name_en")
private String nameEn;
@JSONField(name = "short_name")
private String nameShort;
@JSONField(name = "start_time")
private long startTime;
@JSONField(name = "end_time")
private long endTime;
@JSONField(name = "organizer")
private String organizer;
@JSONField(name = "logo")
private String logo;
@JSONField(name = "address")
private String address;
@JSONField(name = "team_ids")
private List<Integer> teamIds;
@JSONField(name = "status")
private int status;
@Override
public String toString() {
return "League{" +
"leagueId=" + leagueId +
", name='" + name + '\'' +
", nameEn='" + nameEn + '\'' +
", nameShort='" + nameShort + '\'' +
", startTime=" + startTime +
", endTime=" + endTime +
", organizer='" + organizer + '\'' +
", logo='" + logo + '\'' +
", address='" + address + '\'' +
", teamIds=" + teamIds +
", status=" + status +
'}';
}
public void setLeagueId(int leagueId) {
this.leagueId = leagueId;
}
public void setName(String name) {
this.name = name;
}
public void setNameEn(String nameEn) {
this.nameEn = nameEn;
}
public void setNameShort(String nameShort) {
this.nameShort = nameShort;
}
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public void setEndTime(long endTime) {
this.endTime = endTime;
}
public void setOrganizer(String organizer) {
this.organizer = organizer;
}
public void setLogo(String logo) {
this.logo = logo;
}
public void setAddress(String address) {
this.address = address;
}
public void setTeamIds(List<Integer> teamIds) {
this.teamIds = teamIds;
}
public void setStatus(int status) {
this.status = status;
}
}
}
API 返回数据如下(部分):
200
成功
League{leagueId=674, name='2019 亚洲洲际赛', nameEn='Rift Rivals 2019/Asia', nameShort='2019 亚洲洲际赛', startTime=1562169600000, endTime=1562428800000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190620/048a632507ff47918394d5d2c4bcb04c.png', address='韩国首尔', teamIds=[3, 708, 40, 703, 64, 787, 74, 670, 60, 813, 782, 737], status=2}
League{leagueId=675, name='2019 欧美洲际赛', nameEn='2019 RIFT RIVALS', nameShort='2019 欧美洲际赛', startTime=1561564800000, endTime=1561824000000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190620/9ed7e6ab761e436daadf36f9a523b0f7.png', address='洛杉矶', teamIds=[58, 7, 69, 67, 601, 607], status=2}
League{leagueId=671, name='2019 LMS 夏季赛', nameEn='LMS 2019 Summer Split', nameShort='2019 LMS 夏季赛', startTime=1560355200000, endTime=1567094400000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190607/03a5ecfb3ea844a8a111e0df855c6821.png', address='台北', teamIds=[5, 181, 60, 781, 632, 90, 782], status=1}
League{leagueId=672, name='2019 LEC夏季赛', nameEn='LEC 2019 Summer Split', nameShort='2019 LEC夏季赛', startTime=1559836800000, endTime=1567872000000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190531/bf5d954f918644cab9cda6941a89a6c8.png', address='欧洲', teamIds=[965, 669, 607, 67, 686, 601, 825, 824, 62, 660], status=1}
League{leagueId=678, name='2019 LCK夏季赛', nameEn='LCK 2019 Summer Split', nameShort='2019 LCK夏季赛', startTime=1559664000000, endTime=1567094400000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190528/c69270e207ef47d7a8691658ba85ecce.png', address='韩国', teamIds=[46, 703, 4, 708, 2, 49, 40, 44, 704, 3], status=1}
League{leagueId=670, name='2019 LPL夏季赛', nameEn='LPL 2019 Summer Split', nameShort='2019 LPL夏季赛', startTime=1559318400000, endTime=1567180800000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190526/f6f05d4f41e14927bf46cfc97239a1be.png', address='', teamIds=[1, 6, 64, 73, 66, 787, 63, 74, 71, 789, 677, 79, 70, 976, 722, 670], status=1}
League{leagueId=677, name='2019 LCS夏季赛', nameEn='LCS 2019 Summer Split', nameShort='2019 LCS夏季赛', startTime=1559318400000, endTime=1566662400000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190526/d153eca078d44c45baf15676dc8ab270.png', address='北美', teamIds=[779, 7, 771, 68, 602, 680, 773, 778, 58, 69], status=1}
League{leagueId=662, name='2019 MSI季中冠军赛', nameEn='2019 Mid-Season Invitational', nameShort='2019 MSI季中冠军赛', startTime=1556640000000, endTime=1558195200000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190425/cbfad96597dc4391b5e88d117c18720a.png', address='越南 台北', teamIds=[67, 3, 653, 58, 813, 51, 119, 863, 61, 60, 614, 64, 111], status=2}
League{leagueId=669, name='2019NEST电竞大赛', nameEn='2019NEST', nameShort='2019NEST', startTime=1555862400000, endTime=1558800000000, organizer='华奥CGA', logo='http://qn.feijing88.com/feijing-home/egame/image/20190419/40befc4dd6284bf78d9561db2281e226.png', address='贵阳', teamIds=[670, 71, 722, 787, 79, 64, 63, 74, 6, 73, 789, 677, 1, 66, 70], status=2}
League{leagueId=661, name='2019LCK春季升降级赛', nameEn='LCK 2019 Spring Promotion', nameShort='2019LCK春季升降级赛', startTime=1555344000000, endTime=1555776000000, organizer='Riot', logo='http://qn.feijing88.com/feijing-home/egame/image/20190411/1214da36283b411297030116180ac6cf.png', address='韩国', teamIds=[44, 49, 949, 118], status=2}