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

相关推荐
小羊没烦恼!3 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里3 天前
java中的继承和多态的区别
java
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码3 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖3 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时3 天前
01-06-A-JVM排查实战详解
java·jvm
vipxieliang3 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
ba_pi3 天前
mysql 查询所有表名并授权
java
ProcessOn官方账号3 天前
java函数式编程--入门基础
java·编程·函数式编程