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来进行调用获取返回值

相关推荐
寄存器漫游者21 小时前
数据结构 C语言 顺序栈
java·c语言·数据结构
heartbeat..21 小时前
Redis 性能优化全指南:从基础配置到架构升级
java·redis·性能优化·架构
m0_7482331721 小时前
C#与C语言:5大核心语法共性
java·jvm·算法
JavaGuide21 小时前
推荐一个基于 Spring Boot 4.0 + Java 21 + Spring AI 2.0 的大模型项目!
java·spring boot·spring
Maynor99621 小时前
Clawdbot安装教程:从零开始到接入飞书
java·数据库·飞书
小北方城市网21 小时前
Spring Boot 多数据源与事务管理实战:主从分离、动态切换与事务一致性
java·开发语言·jvm·数据库·mysql·oracle·mybatis
roman_日积跬步-终至千里1 天前
【Java 并发-面试】从线程基础到企业级开发的知识点概况
java·开发语言
m0_748233171 天前
C与C++:底层编程的六大核心共性
java·开发语言
坊钰1 天前
【Rabbit MQ】Rabbit MQ 介绍
java·rabbitmq
雀啼春1 天前
Java中的数据类型
java