足球数据API接口 - 【比赛动画直播数据】API调用示例代码

分享接口调用的示例代码,篮球数据【比赛动画直播数据】接口

package com.huaying.demo.football;

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;
import java.util.stream.Collectors;

/**
 * 37.比赛动画直播数据
 *
 */
public class FootballAnimationLive {

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

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

            ResultList list = (ResultList) unmarshaller.unmarshal(new ByteArrayInputStream(content.getBytes()));
            list.getResultList().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/FootballAnimationLive.xml"), StandardCharsets.UTF_8);
            lines.forEach(line -> builder.append(line));
            return builder.toString();
        } catch (Throwable t) {
            t.printStackTrace();
            return "";
        }
    }

    @XmlRootElement(name = "c")
    public static class ResultList {
        @XmlElement(name = "match")
        private List<AnimationLive> itemList;

        public List<AnimationLive> getResultList() {
            return itemList.stream()
                    .map(AnimationLive::parse)
                    .collect(Collectors.toList());
        }
    }

    private static class AnimationLive {
        @XmlElement(name = "f")
        private String data;

        private String matchInfo;
        private String lastAnimationLive;
        private String eventData;

        public AnimationLive parse() {
            String[] values = data.split("!");

            matchInfo = getData(values, 0);
            lastAnimationLive = getData(values, 1);
            eventData = getData(values, 2);
            return this;
        }

        private String getData(String[] values, int index) {
            if (index >= 0 && index < values.length) {
                return values[index];
            } else {
                return null;
            }
        }

        @Override
        public String toString() {
            return "AnimationLive{" +
                    "matchInfo='" + matchInfo + '\'' +
                    ", lastAnimationLive='" + lastAnimationLive + '\'' +
                    ", eventData='" + eventData + '\'' +
                    '}';
        }
    }
}

API 返回数据如下(部分): 


AnimationLive{matchInfo='1662408^雷暴^28℃~29℃^味之素体育场,日立柏足球場^200^11943^4^0^-1^47^2019,8,8,16,12,02^2019,8,8,16,26,17', lastAnimationLive='519,0,4,,-1,90,', eventData='4,1,11943,20,7^8,1,11943,20,7^14,1,11943,20,7^17,1,200,20,7^20,1,200,20,7^25,1,200,20,7^29,1,11943,20,7^34,1,11943,20,7^40,1,11943,20,7^47,1,200,20,7^58,1,200,20,7^67,1,11943,20,7^72,1,11943,20,7^76,1,200,20,9^80,1,11943,20,10^84,1,200,20,11^87,1,11943,20,11^97,1,200,20,14^103,1,11943,20,14^110,1,200,20,15^113,1,11943,20,16^118,1,200,20,17^125,1,1194
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值