基于[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();
    }
}
相关推荐
卓码软件测评2 小时前
第三方软件验收评测机构【Gatling安装指南:Java环境配置和IDE插件安装】
java·开发语言·ide·测试工具·负载均衡
妮妮分享2 小时前
H5获取定位的方式是什么?
java·前端·javascript
李小星同志2 小时前
DPO,PPO,GRPO的学习
人工智能·深度学习·学习
Billow_lamb2 小时前
MyBatis-Plus 的 条件构造器详解(超详细版)
java·mybatis
CoderYanger2 小时前
动态规划算法-两个数组的dp(含字符串数组):48.最长重复子数组
java·算法·leetcode·动态规划·1024程序员节
西召3 小时前
Spring Kafka 动态消费实现案例
java·后端·kafka
镜花水月linyi3 小时前
ThreadLocal 深度解析(上)
java·后端
镜花水月linyi3 小时前
ThreadLocal 深度解析(下)
java·后端
她说..3 小时前
Spring AOP场景2——数据脱敏(附带源码)
java·开发语言·java-ee·springboot·spring aop