java发url请求获取前一天天气信息20230402发布

之前发的方法已经不太好用了。

只好再研究个新方法了:

1.使用网址截图:


2.代码

//导包
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;

//主体
        String weatherUrl = "https://h5ctywhr.api.moji.com/weatherDetail";
        String jsonParam = "{"cityId":"1909","cityType":0}";//1909是太原,其他地区可从网站点进去,抄它url中写的代码
        HttpResponse httpResponse =
                HttpRequest.post(weatherUrl)
                        .header(Header.USER_AGENT,"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36")
                        .body(jsonParam)
                        .timeout(6000)
                        .execute();
        log.debug(httpResponse.toString());

        String respBody = httpResponse.body();
        JSONArray ja = JSONUtil.parseObj(respBody)
                .getJSONObject("forecastDays").getJSONArray("forecastDay");
        log.debug("respJson:" + ja.toString());

        for(int i=0; i

页面更新:2024-05-01

标签:天气   太原   截图   其他地区   新方法   气温   主体   日期   单位   代码   信息

1 2 3 4 5

上滑加载更多 ↓
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top