足球數據【盃賽聯賽資料】API調用示例代碼

分享下之前調用過的足球盃賽聯賽資料代碼,用的是feijing88.com 的API接口

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.盃賽聯賽資料
 * @Website: https://www.feijing88.com
 */
public class FootballLeagueInfo {

    public static void main(String[] args) {
        try {
            String content = getContent();

            JAXBContext jaxbContext = JAXBContext.newInstance(LeagueList.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

            LeagueList leagueList = (LeagueList) unmarshaller.unmarshal(new ByteArrayInputStream(content.getBytes()));
            leagueList.getLeagueList().forEach(item -> System.out.println(item));

        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    /**
     * 獲取API返回內容
     *
     * Note: 這裏爲了方便測試我使用了一份本地文件,使用時應替換爲真實接口返回內容
     */
    private static String getContent() {
        try {
            StringBuilder builder = new StringBuilder();
            List<String> lines = Files.readAllLines(Paths.get("./src/main/resources/FootballLeagueInfo.xml"), StandardCharsets.UTF_8);
            lines.forEach(line -> builder.append(line));
            return builder.toString();
        } catch (Throwable t) {
            t.printStackTrace();
            return "";
        }
    }

    @XmlRootElement(name = "list")
    public static class LeagueList {
        @XmlElement(name = "match")
        private List<League> leagueList;

        public List<League> getLeagueList() {
            return leagueList;
        }
    }

    @XmlRootElement
    public static class League {
        @XmlElement
        private String id;
        @XmlElement
        private String gb_short;
        @XmlElement
        private String big_short;
        @XmlElement
        private String en_short;
        @XmlElement
        private String gb;
        @XmlElement
        private String big;
        @XmlElement
        private String en;
        @XmlElement
        private int type;
        @XmlElement
        private String subSclass;
        @XmlElement
        private int sum_round;
        @XmlElement
        private int curr_round;
        @XmlElement
        private String Curr_matchSeason;
        @XmlElement
        private String countryID;
        @XmlElement
        private String country;
        @XmlElement
        private String areaID;
        @XmlElement
        private String countryEn;
        @XmlElement
        private String logo;
        @XmlElement
        private String countryLogo;

        @Override
        public String toString() {
            return "League{" +
                    "id='" + id + '\'' +
                    ", gb_short='" + gb_short + '\'' +
                    ", big_short='" + big_short + '\'' +
                    ", en_short='" + en_short + '\'' +
                    ", gb='" + gb + '\'' +
                    ", big='" + big + '\'' +
                    ", en='" + en + '\'' +
                    ", type=" + type +
                    ", subSclass='" + subSclass + '\'' +
                    ", sum_round=" + sum_round +
                    ", curr_round=" + curr_round +
                    ", Curr_matchSeason='" + Curr_matchSeason + '\'' +
                    ", countryID='" + countryID + '\'' +
                    ", country='" + country + '\'' +
                    ", areaID='" + areaID + '\'' +
                    ", countryEn='" + countryEn + '\'' +
                    ", logo='" + logo + '\'' +
                    ", countryLogo='" + countryLogo + '\'' +
                    '}';
        }

        public String getId() {
            return id;
        }

        public String getGb_short() {
            return gb_short;
        }

        public String getBig_short() {
            return big_short;
        }

        public String getEn_short() {
            return en_short;
        }

        public String getGb() {
            return gb;
        }

        public String getBig() {
            return big;
        }

        public String getEn() {
            return en;
        }

        public int getType() {
            return type;
        }

        public String getSubSclass() {
            return subSclass;
        }

        public int getSum_round() {
            return sum_round;
        }

        public int getCurr_round() {
            return curr_round;
        }

        public String getCurr_matchSeason() {
            return Curr_matchSeason;
        }

        public String getCountryID() {
            return countryID;
        }

        public String getCountry() {
            return country;
        }

        public String getAreaID() {
            return areaID;
        }

        public String getCountryEn() {
            return countryEn;
        }

        public String getLogo() {
            return logo;
        }

        public String getCountryLogo() {
            return countryLogo;
        }
    }
}

返回數據如下:

League{id='1', gb_short='愛超', big_short='愛超', en_short='IRE PR', gb='愛爾蘭超級聯賽', big='愛爾蘭超級聯賽', en='Ireland Premier Division', type=1, subSclass='聯賽', sum_round=36, curr_round=20, Curr_matchSeason='2019', countryID='16', country='愛爾蘭', areaID='1', countryEn='Ireland', logo='http://zq.win007.com/Image/league_match/images/20181129190115.png', countryLogo='http://zq.win007.com/Image/info/images/200712304261564347.gif'}
League{id='2', gb_short='阿甲', big_short='阿甲', en_short='ARG D1', gb='阿根廷甲級聯賽', big='阿根廷甲級聯賽', en='Argentine Division 1', type=1, subSclass='聯賽', sum_round=25, curr_round=25, Curr_matchSeason='2018-2019', countryID='38', country='阿根廷', areaID='2', countryEn='Argentina', logo='http://zq.win007.com/Image/league_match/images/20130917105542.jpg', countryLogo='http://zq.win007.com/Image/info/images/20130121182844.jpg'}
League{id='3', gb_short='奧甲', big_short='奧甲', en_short='AUT D1', gb='奧地利甲組聯賽', big='奧地利甲組聯賽', en='Austrian Bundesliga', type=1, subSclass='聯賽', sum_round=22, curr_round=1, Curr_matchSeason='2019-2020', countryID='14', country='奧地利', areaID='1', countryEn='Austria', logo='http://zq.win007.com/Image/league_match/images/20140122115024.jpg', countryLogo='http://zq.win007.com/Image/info/images/20130801165112.jpg'}
League{id='4', gb_short='巴西甲', big_short='巴西甲', en_short='BRA D1', gb='巴西甲組聯賽', big='巴西甲組聯賽', en='Brazil Serie A', type=1, subSclass='null', sum_round=38, curr_round=10, Curr_matchSeason='2019', countryID='39', country='巴西', areaID='2', countryEn='Brazil', logo='http://zq.win007.com/Image/league_match/images/20170507175810.png', countryLogo='http://zq.win007.com/Image/info/images/20130121200051.jpg'}
League{id='5', gb_short='比甲', big_short='比甲', en_short='BEL D1', gb='比利時甲組聯賽', big='比利時甲組聯賽', en='Belgian Pro League', type=1, subSclass='聯賽', sum_round=30, curr_round=1, Curr_matchSeason='2019-2020', countryID='9', country='比利時', areaID='1', countryEn='Belgium', logo='http://zq.win007.com/Image/league_match/images/20181204180011.png', countryLogo='http://zq.win007.com/Image/info/images/20130121180759.jpg'}
League{id='6', gb_short='波蘭超', big_short='波蘭超', en_short='POL PR', gb='波蘭超級聯賽', big='波蘭超級聯賽', en='Poland Ekstraklasa', type=1, subSclass='聯賽', sum_round=30, curr_round=1, Curr_matchSeason='2019-2020', countryID='19', country='波蘭', areaID='1', countryEn='Poland', logo='http://zq.win007.com/Image/league_match/images/20160619101249.jpg', countryLogo='http://zq.win007.com/Image/info/images/20130121175428.jpg'}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章