分享接口调用的示例代码,篮球数据【比赛动画直播数据】接口
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