電競英雄聯盟數據API接口 - 【選手基本信息】API調用示例代碼

分享使用 飛鯨體育數據 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 LolPlayer {

    public static void main(String[] args) {
        try {
            String content = getContent();
            Respond rsp = JSON.parseObject(content, Respond.class);
            System.out.println(rsp);

        } 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/LolPlayer.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 Player data;

        @Override
        public String toString() {
            return "Respond{" +
                    "code=" + code +
                    ", message='" + message + '\'' +
                    ", data=" + data +
                    '}';
        }

        public void setCode(int code) {
            this.code = code;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        public void setData(Player data) {
            this.data = data;
        }
    }

    public static class Player {

        @JSONField
        private int playerId;
        @JSONField
        private int teamId;
        @JSONField
        private String avatar;
        @JSONField
        private String nickName;
        @JSONField
        private String realName;
        @JSONField
        private String position;
        @JSONField
        private String country;
        @JSONField
        private String introduction;

        @Override
        public String toString() {
            return "Player{" +
                    "playerId=" + playerId +
                    ", teamId=" + teamId +
                    ", avatar='" + avatar + '\'' +
                    ", nickName='" + nickName + '\'' +
                    ", realName='" + realName + '\'' +
                    ", position='" + position + '\'' +
                    ", country='" + country + '\'' +
//                    ", introduction='" + introduction + '\'' +
                    '}';
        }

        public void setPlayerId(int playerId) {
            this.playerId = playerId;
        }

        public void setTeamId(int teamId) {
            this.teamId = teamId;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }

        public void setNickName(String nickName) {
            this.nickName = nickName;
        }

        public void setRealName(String realName) {
            this.realName = realName;
        }

        public void setPosition(String position) {
            this.position = position;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public void setIntroduction(String introduction) {
            this.introduction = introduction;
        }
    }

}

API 返回數據如下(部分):

Respond{code=200, message='成功', data=Player{playerId=1, teamId=6, avatar='https://qn.feijing88.com/feijing-home/egame/image/20190630/6e87a18768004d6392bd3e656bacc9a8.png', nickName='clearlove', realName='明凱', position='打野', country='中國'}}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章