基于[api-football]数据学习示例

基于api-football数据学习示例

java 复制代码
import java.net.*;
import java.io.*;

/**
 * Football API 学习示例 - 最简版本
 */
public class LearnFootballAPI {
    
    private static final String BASE_URL = "https://v3.football.api-sports.io";
    private String apiKey;
    
    public LearnFootballAPI(String apiKey) {
        this.apiKey = apiKey;
    }
    
    /**
     * 发送GET请求
     */
    public String get(String path, String query) {
        try {
            // 1. 构建URL
            String urlStr = BASE_URL + path;
            if (query != null && !query.isEmpty()) {
                urlStr += "?" + query;
            }
            
            // 2. 创建连接
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            
            // 3. 设置请求头
            conn.setRequestMethod("GET");
            conn.setRequestProperty("x-rapidapi-key", apiKey);
            conn.setRequestProperty("x-rapidapi-host", "v3.football.api-sports.io");
            
            // 4. 获取响应
            int code = conn.getResponseCode();
            if (code != 200) {
                return "Error " + code + ": " + conn.getResponseMessage();
            }
            
            // 5. 读取响应数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            
            return response.toString();
            
        } catch (Exception e) {
            return "Exception: " + e.getMessage();
        }
    }
    
    /**
     * 示例1: 查询所有国家
     */
    public void demoCountries() {
        System.out.println("=== 查询所有国家 ===");
        String result = get("/countries", "");
        System.out.println(result);
    }
    
    /**
     * 示例2: 查询英超
     */
    public void demoPremierLeague() {
        System.out.println("\n=== 查询英超 ===");
        String result = get("/leagues", "id=39");
        System.out.println(result);
    }
    
    /**
     * 示例3: 查询英超积分榜
     */
    public void demoStandings() {
        System.out.println("\n=== 查询英超积分榜 ===");
        String result = get("/standings", "league=39&season=2023");
        System.out.println(result);
    }
    
    /**
     * 示例4: 查询比赛
     */
    public void demoFixtures() {
        System.out.println("\n=== 查询今日比赛 ===");
        String today = java.time.LocalDate.now().toString();
        String result = get("/fixtures", "league=39&season=2023&date=" + today);
        System.out.println(result);
    }
    
    /**
     * 示例5: 查询球员
     */
    public void demoPlayers() {
        System.out.println("\n=== 查询梅西 ===");
        String result = get("/players", "search=messi");
        System.out.println(result);
    }
    
    /**
     * 运行所有示例
     */
    public void runAllExamples() {
        demoCountries();
        demoPremierLeague();
        demoStandings();
        demoFixtures();
        demoPlayers();
    }
    
    public static void main(String[] args) {
        // 替换为你的API Key
        String apiKey = "YOUR_API_KEY_HERE";
        
        LearnFootballAPI api = new LearnFootballAPI(apiKey);
        api.runAllExamples();
    }
}
相关推荐
weixin_425023004 分钟前
Java开发高频实用技巧汇总(List操作/多线程/反射/监控等)
java·windows·list
alonewolf_991 小时前
深入Spring核心原理:从Bean生命周期到AOP动态代理全解析
java·后端·spring
天远Date Lab1 小时前
Python实现用户消费潜力评估:天远个人消费能力等级API对接全攻略
java·大数据·网络·python
科技林总1 小时前
【系统分析师】1.1 信息与信息系统
学习
HyperAI超神经5 小时前
在线教程丨 David Baker 团队开源 RFdiffusion3,实现全原子蛋白质设计的生成式突破
人工智能·深度学习·学习·机器学习·ai·cpu·gpu
没有bug.的程序员8 小时前
服务安全:内部服务如何防止“裸奔”?
java·网络安全·云原生安全·服务安全·零信任架构·微服务安全·内部鉴权
一线大码9 小时前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
YJlio9 小时前
VolumeID 学习笔记(13.10):卷序列号修改与资产标识管理实战
windows·笔记·学习
weixin_440730509 小时前
java数组整理笔记
java·开发语言·笔记
小龙9 小时前
【学习笔记】多标签交叉熵损失的原理
笔记·学习·多标签交叉熵损失