基于[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();
    }
}
相关推荐
FQNmxDG4S26 分钟前
Java多线程编程:Thread与Runnable的并发控制
java·开发语言
Slow菜鸟27 分钟前
AI学习篇(五) | awesome-design-md 使用说明
人工智能·学习
狐狐生风1 小时前
LangChain 向量存储:Chroma、FAISS
人工智能·python·学习·langchain·faiss·agentai
虹科网络安全1 小时前
艾体宝干货|数据复制详解:类型、原理与适用场景
java·开发语言·数据库
狐狐生风1 小时前
LangChain RAG 基础
人工智能·python·学习·langchain·rag·agentai
axng pmje2 小时前
Java语法进阶
java·开发语言·jvm
rKWP8gKv72 小时前
Java微服务性能监控:Prometheus与Grafana集成方案
java·微服务·prometheus
老前端的功夫2 小时前
【Java从入门到入土】28:Stream API:告别for循环的新时代
java·开发语言·python
qq_435287922 小时前
第9章 夸父逐日与后羿射日:死循环与进程终止?十个太阳同时值班的并行冲突
java·开发语言·git·死循环·进程终止·并行冲突·夸父逐日
小江的记录本2 小时前
【Kafka核心】架构模型:Producer、Broker、Consumer、Consumer Group、Topic、Partition、Replica
java·数据库·分布式·后端·搜索引擎·架构·kafka