基于[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();
    }
}
相关推荐
皮皮林5513 小时前
Java性能调优黑科技!1行代码实现毫秒级耗时追踪,效率飙升300%!
java
冰_河3 小时前
QPS从300到3100:我靠一行代码让接口性能暴涨10倍,系统性能原地起飞!!
java·后端·性能优化
桦说编程6 小时前
从 ForkJoinPool 的 Compensate 看并发框架的线程补偿思想
java·后端·源码阅读
躺平大鹅8 小时前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者8 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺8 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart10 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP11 小时前
MyBatis-mybatis入门与增删改查
java
孟陬14 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端