我是靠谱客的博主 聪慧豌豆,这篇文章主要介绍篮球数据API接口 - 【实时比分数据】API调用示例代码,现在分享给大家,希望可以做个参考。

野子电竞数据官网改版https://www.xxe.io/全新登场
package com.huaying.demo.basketball;
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;

/**

  • @API: 2.即时变化的比分数据

  • @Website: https://www.xxe.io/
    */
    public class BasketballChange {

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

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    JAXBContext jaxbContext = JAXBContext.newInstance(ChangeList.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); ChangeList list = (ChangeList) unmarshaller.unmarshal(new ByteArrayInputStream(content.getBytes())); list.getChangeList().forEach(System.out::println); } catch (Throwable t) { t.printStackTrace(); }

    }

    /**

    • 获取API返回内容
    • Note: 这里为了方便测试我使用了一份本地文件,使用时应替换为真实接口返回内容
      */
      private static String getContent() {
      try {
      StringBuilder builder = new StringBuilder();
      List lines = Files.readAllLines(Paths.get("./src/main/resources/BasketballChange.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 ChangeList {
    @XmlElement(name = “h”)
    private List itemList;

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    public List<Change> getChangeList() { return itemList.stream().map(s -> { Change change = new Change(); change.parse(s); return change; }).collect(Collectors.toList()); }

    }

    public static class Change {
    private String matchId;
    private int matchStatus;
    private String remainTime;
    private int homeScore;
    private int homeScoreFirst;
    private int homeScoreSecond;
    private int homeScoreThird;
    private int homeScoreFourth;
    private int homeScoreFirstOT;
    private int homeScoreSecondOT;
    private int homeScoreThirdOT;
    private int awayScore;
    private int awayScoreFirst;
    private int awayScoreSecond;
    private int awayScoreThird;
    private int awayScoreFourth;
    private int awayScoreFirstOT;
    private int awayScoreSecondOT;
    private int awayScoreThirdOT;

    复制代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    public void parse(String data) { String[] values = data.split("\^"); matchId = values[0]; matchStatus = parseInt(values[1]); remainTime = values[2]; homeScore = parseInt(values[3]); homeScoreFirst = parseInt(values[5]); homeScoreSecond = parseInt(values[7]); homeScoreThird = parseInt(values[9]); homeScoreFourth = parseInt(values[11]); homeScoreFirstOT = parseInt(values[16]); homeScoreSecondOT = parseInt(values[18]); homeScoreThirdOT = parseInt(values[20]); awayScore = parseInt(values[4]); awayScoreFirst = parseInt(values[6]); awayScoreSecond = parseInt(values[8]); awayScoreThird = parseInt(values[10]); awayScoreFourth = parseInt(values[12]); awayScoreFirstOT = parseInt(values[17]); awayScoreSecondOT = parseInt(values[19]); awayScoreThirdOT = parseInt(values[21]); } private int parseInt(String data) { return data == null || data.isEmpty() ? 0 : Integer.valueOf(data); } @Override public String toString() { return "Change{" + "matchId='" + matchId + ''' + ", matchStatus=" + matchStatus + ", remainTime='" + remainTime + ''' + ", homeScore=" + homeScore + ", homeScoreFirst=" + homeScoreFirst + ", homeScoreSecond=" + homeScoreSecond + ", homeScoreThird=" + homeScoreThird + ", homeScoreFourth=" + homeScoreFourth + ", homeScoreFirstOT=" + homeScoreFirstOT + ", homeScoreSecondOT=" + homeScoreSecondOT + ", homeScoreThirdOT=" + homeScoreThirdOT + ", awayScore=" + awayScore + ", awayScoreFirst=" + awayScoreFirst + ", awayScoreSecond=" + awayScoreSecond + ", awayScoreThird=" + awayScoreThird + ", awayScoreFourth=" + awayScoreFourth + ", awayScoreFirstOT=" + awayScoreFirstOT + ", awayScoreSecondOT=" + awayScoreSecondOT + ", awayScoreThirdOT=" + awayScoreThirdOT + '}'; }

    }
    }

最后

以上就是聪慧豌豆最近收集整理的关于篮球数据API接口 - 【实时比分数据】API调用示例代码的全部内容,更多相关篮球数据API接口内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部