分享下英雄联盟LOL接口调用的示例代码,查看文档
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 LolHero {
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/LolHero.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<Hero> 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<Hero> getData() {
return data;
}
public void setData(List<Hero> data) {
this.data = data;
}
}
public static class Hero {
@JSONField
private int heroId;
@JSONField
private int leagueId;
@JSONField
private double kda;
@JSONField
private double killAverage;
@JSONField
private double deathAverage;
@JSONField
private double assistAverage;
@JSONField
private String position;
@JSONField
private double pickRate;
@JSONField
private double banRate;
@JSONField
private double winRate;
@Override
public String toString() {
return "Hero{" +
"heroId=" + heroId +
", leagueId=" + leagueId +
", kda=" + kda +
", killAverage=" + killAverage +
", deathAverage=" + deathAverage +
", assistAverage=" + assistAverage +
", position='" + position + '\'' +
", pickRate=" + pickRate +
", banRate=" + banRate +
", winRate=" + winRate +
'}';
}
public void setHeroId(int heroId) {
this.heroId = heroId;
}
public void setLeagueId(int leagueId) {
this.leagueId = leagueId;
}
public void setKda(double kda) {
this.kda = kda;
}
public void setKillAverage(double killAverage) {
this.killAverage = killAverage;
}
public void setDeathAverage(double deathAverage) {
this.deathAverage = deathAverage;
}
public void setAssistAverage(double assistAverage) {
this.assistAverage = assistAverage;
}
public void setPosition(String position) {
this.position = position;
}
public void setPickRate(double pickRate) {
this.pickRate = pickRate;
}
public void setBanRate(double banRate) {
this.banRate = banRate;
}
public void setWinRate(double winRate) {
this.winRate = winRate;
}
}
}
API 返回数据如下(部分):
成功
Hero{heroId=1, leagueId=51, kda=4.0, killAverage=0.2, deathAverage=0.1, assistAverage=0.3, position='中单', pickRate=5.882, banRate=0.0, winRate=71.429}
Hero{heroId=1, leagueId=51, kda=4.0, killAverage=0.2, deathAverage=0.1, assistAverage=0.3, position='中单', pickRate=5.882, banRate=0.0, winRate=71.429}
Hero{heroId=2, leagueId=51, kda=3.6, killAverage=0.0, deathAverage=0.1, assistAverage=0.2, position='中单', pickRate=1.681, banRate=0.0, winRate=50.0}
Hero{heroId=3, leagueId=51, kda=2.4, killAverage=0.1, deathAverage=0.1, assistAverage=0.2, position='上单', pickRate=5.882, banRate=0.0, winRate=57.143}
Hero{heroId=3, leagueId=51, kda=2.4, killAverage=0.1, deathAverage=0.1, assistAverage=0.2, position='上单', pickRate=5.882, banRate=0.0, winRate=57.143}
Hero{heroId=4, leagueId=51, kda=3.0, killAverage=0.2, deathAverage=1.0, assistAverage=2.9, position='辅助', pickRate=42.017, banRate=52.941, winRate=56.0}
Hero{heroId=6, leagueId=51, kda=15.0, killAverage=0.0, deathAverage=0.0, assistAverage=0.1, position='中单', pickRate=0.84, banRate=3.361, winRate=100.0}
Hero{heroId=8, leagueId=51, kda=2.4, killAverage=0.0, deathAverage=0.0, assistAverage=0.1, position='ADC', pickRate=1.681, banRate=0.84, winRate=0.0}
Hero{heroId=10, leagueId=51, kda=6.8, killAverage=0.2, deathAverage=0.1, assistAverage=0.3, position='中单', pickRate=6.723, banRate=2.521, winRate=62.5}
Hero{heroId=11, leagueId=51, kda=1.5, killAverage=0.0, deathAverage=0.0, assistAverage=0.0, position='辅助', pickRate=0.84, banRate=0.0, winRate=0.0}
Hero{heroId=13, leagueId=51, kda=2.4, killAverage=0.0, deathAverage=0.0, assistAverage=0.1, position='ADC', pickRate=0.84, banRate=0.0, winRate=0.0}
Hero{heroId=14, leagueId=51, kda=3.4, killAverage=0.1, deathAverage=0.6, assistAverage=2.0, position='辅助', pickRate=28.571, banRate=9.244, winRate=50.0}
Hero{heroId=15, leagueId=51, kda=10.0, killAverage=0.0, deathAverage=0.0, assistAverage=0.1, position='ADC', pickRate=0.84, banRate=0.0, winRate=0.0}