java获取天气信息

java获取天气信息

  • 注册后获取API Key
java 复制代码
https://www.visualcrossing.com/
  • 引入依赖
java 复制代码
<dependencies>
    <!-- HTTP 请求 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>

    <!-- CSV 解析 -->
    <dependency>
        <groupId>com.opencsv</groupId>
        <artifactId>opencsv</artifactId>
        <version>5.5.2</version>
    </dependency>

    <!-- Excel 生成 -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.2</version>
    </dependency>
</dependencies>
  • demo
java 复制代码
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;

import com.opencsv.CSVReader;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class RainfallFetcher {
    public static void main(String[] args) throws Exception {
        String apiKey = "RM3T8MSZBZL4H6E7TAXQYZ6MW";
        // 使用坐标:纬度36.8556,经度114.5030,逗号必须URL编码成%2C
        String location = "36.8556%2C114.5030";

        String startDate = "2024-06-16";
        String endDate = "2025-06-16";

        String urlStr = String.format(
            "https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/%s/%s/%s?unitGroup=metric&elements=datetime,precip&include=days&key=%s&contentType=csv",
            location, startDate, endDate, apiKey);

        System.out.println("请求URL: " + urlStr);

        try (
            InputStream inputStream = new URL(urlStr).openStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
            CSVReader csvReader = new CSVReader(inputStreamReader);
            Workbook workbook = new XSSFWorkbook();
            FileOutputStream fileOut = new FileOutputStream("Shahe_Rain_Over_5mm.xlsx")
        ) {
            String[] header = csvReader.readNext();  // 读取表头 ["datetime", "precip"]
            Sheet sheet = workbook.createSheet("降雨≥5mm");
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < header.length; i++) {
                headerRow.createCell(i).setCellValue(header[i]);
            }

            int rowIndex = 1;
            String[] line;
            while ((line = csvReader.readNext()) != null) {
                double precip = Double.parseDouble(line[1]);
                if (precip >= 5.0) {
                    Row row = sheet.createRow(rowIndex++);
                    row.createCell(0).setCellValue(line[0]);
                    row.createCell(1).setCellValue(precip);
                }
            }
            workbook.write(fileOut);
            System.out.println("导出完成,共找到 " + (rowIndex - 1) + " 天降雨≥5mm。");
        }
    }
}

通过java代码可以直接获取到csv文件。不过我使用的是将打印出来的完整请求地址,放入到postman中,使用postman来进行调用获取返回值

相关推荐
lifewange20 小时前
Claude Code可以安装在IDEA和Pycharm中么
java·pycharm·intellij-idea
lifewange20 小时前
OpenCode可以安装在IDEA和Pycharm中么
java·pycharm·intellij-idea
untE EADO20 小时前
Java进阶之路,Java程序员职业发展规划
java·开发语言
长河21 小时前
基于 Jib 实现无 Dockerfile 的 Spring Boot 应用容器化
java·spring boot·后端
Maiko Star21 小时前
Spring AI ChatClient 完全指南:从基础配置到流式调用
java·人工智能·spring
l1t21 小时前
类似 X-13ARIMA-SEATS 功能的 JDemetra+ 安装和使用
java·数据库·r语言
架构源启21 小时前
2026 进阶篇:深入理解Spring Reactor响应式编程的核心引擎(源码级解析+实战避坑)
java·后端·spring
薪火铺子21 小时前
SpringMVC请求处理流程源码解析(第2篇):处理器执行与参数绑定
java·后端·spring
SamDeepThinking21 小时前
一个跑了三年没出过问题的系统,我是怎么设计的
java·后端·架构
逸Y 仙X21 小时前
文章十七:ElasticSearch get\search查询相关参数
java·大数据·elasticsearch·搜索引擎·全文检索