分享使用 飛鯨體育數據 www.feijing88.com 接口調用的示例代碼,今天接的是英雄聯盟的【戰隊列表】接口.
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: 戰隊列表
* @Website: https://www.feijing88.com
*/
public class LolTeam {
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.getData().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/LolTeam.json"), StandardCharsets.UTF_8);
lines.forEach(builder::append);
return builder.toString();
} catch (Throwable t) {
t.printStackTrace();
return "";
}
}
public static class Respond {
@JSONField
private int code;
@JSONField
private String message;
@JSONField
private List<Team> data;
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<Team> getData() {
return data;
}
public void setData(List<Team> data) {
this.data = data;
}
}
public static class Team {
@JSONField
private int teamId;
@JSONField
private String name;
@JSONField
private String logo;
@JSONField
private String nameEn;
@JSONField
private String shortName;
@JSONField
private String introduction;
@Override
public String toString() {
return "Hero{" +
"teamId=" + teamId +
", name='" + name + '\'' +
", logo='" + logo + '\'' +
", nameEn='" + nameEn + '\'' +
", shortName='" + shortName + '\'' +
// ", introduction='" + introduction + '\'' +
'}';
}
public void setTeamId(int teamId) {
this.teamId = teamId;
}
public void setName(String name) {
this.name = name;
}
public void setLogo(String logo) {
this.logo = logo;
}
public void setNameEn(String nameEn) {
this.nameEn = nameEn;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}
public void setIntroduction(String introduction) {
this.introduction = introduction;
}
}
}
API 返回數據如下(部分):
200
成功
Team{teamId=1, name='RNG電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20190712/0f818524cc124667947977ba4b5d1c49.png', nameEn='Royal Never Giveup', shortName='RNG'}
Team{teamId=2, name='HLE電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20190123/5294118736f14232a9c254e0cbc863f5.jpg', nameEn='Hanwha Life Esports', shortName='HLE'}
Team{teamId=3, name='SKT電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20190606/04c1a3bb06bf417b9dd0e9d57a609ce9.png', nameEn='SK TELECOM T1', shortName='SKT'}
Team{teamId=3, name='SKT電子競技俱樂部', logo='https://qn.feijing88.com/feijing-home/egame/image/20190606/04c1a3bb06bf417b9dd0e9d57a609ce9.png', nameEn='SK TELECOM T1', shortName='SKT'}
Team{teamId=4, name='Gen電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20190123/ad74c7fe698c477da52cf938e577b612.jpg', nameEn='Gen.G', shortName='GEN'}
Team{teamId=5, name='AHQ電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20190126/86a57191728745c08162861368f1a553.jpg', nameEn='ahq e-Sports Club', shortName='AHQ'}
Team{teamId=6, name='EDG電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20190711/1492443d4caa4fec98d42f0013ce1fd6.png', nameEn='Edward Gaming', shortName='EDG'}
Team{teamId=7, name='C9電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20190126/aa77266e32714030bcac520cc5238b15.jpg', nameEn='Cloud9', shortName='C9'}
Team{teamId=8, name='H2k電子競技俱樂部', logo='https://qn.feijing88.com/feijing-home/egame/image/20190630/f75078d0de2a40bd9abd77d760597e5b.png', nameEn='H2k-Gaming', shortName='H2K'}
Team{teamId=9, name='M19電子競技俱樂部', logo='http://qn.feijing88.com/feijing-home/egame/image/20181126/abe251f62f1c4cd4b69a424a0b7d6f8d.jpg', nameEn='M19', shortName='M19'}