官网:https://dashboard.juhe.cn/

先实名认证,认证通过后,看要哪个API 点击“申请新数据” 申请通过之后就可以得到key了。。

 

输入框输入“天气预报”

点击立即申请后,就会生成key。

天气预报API接口

1、权威渠道,准确性高
2、可查询当前天气,未来数天天气
3、覆盖面广,覆盖并精确到全国区县及以上城市

1、根据经纬度查询天气

(1)、个人账号调用天气预报接口:http://apis.juhe.cn/simpleWeather/query?key=你的KEY&city=%E6%AD%A6%E6%B1%89

返回的结果如下:

{
    "reason": "查询成功!",
    "result": {
        "city": "武汉",
        "realtime": {
            "temperature": "19",
            "humidity": "23",
            "info": "",
            "wid": "00",
            "direct": "西南风",
            "power": "3级",
            "aqi": "81"
        },
        "future": [
            {
                "date": "2023-03-13",
                "temperature": "2/20℃",
                "weather": "",
                "wid": {
                    "day": "00",
                    "night": "00"
                },
                "direct": "持续无风向"
            },
            {
                "date": "2023-03-14",
                "temperature": "9/25℃",
                "weather": "多云",
                "wid": {
                    "day": "01",
                    "night": "01"
                },
                "direct": "南风转持续无风向"
            },
            {
                "date": "2023-03-15",
                "temperature": "11/24℃",
                "weather": "多云转阴",
                "wid": {
                    "day": "01",
                    "night": "02"
                },
                "direct": "东北风"
            },
            {
                "date": "2023-03-16",
                "temperature": "8/21℃",
                "weather": "小雨转中雨",
                "wid": {
                    "day": "07",
                    "night": "08"
                },
                "direct": "持续无风向"
            },
            {
                "date": "2023-03-17",
                "temperature": "7/10℃",
                "weather": "小雨转阴",
                "wid": {
                    "day": "07",
                    "night": "02"
                },
                "direct": "北风转持续无风向"
            }
        ]
    },
    "error_code": 0
}

(2)、企业账号postman查询武汉的天气:http://v.juhe.cn/weather/geo?format=2&key=保密&lon=114.31667&lat=30.51667

返回数据

{
    "resultcode": "200",
    "reason": "successed!",
    "result": {
        "sk": {
            "temp": "14",
            "wind_direction": "南风",
            "wind_strength": "3级",
            "humidity": "33%",
            "time": "09:59"
        },
        "today": {
            "temperature": "2℃~20℃",
            "weather": "",
            "weather_id": {
                "fa": "00",
                "fb": "00"
            },
            "wind": "持续无风向微风",
            "week": "星期一",
            "city": "武汉",
            "date_y": "2023年03月13日",
            "dressing_index": "较舒适",
            "dressing_advice": "建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
            "uv_index": "中等",
            "comfort_index": "",
            "wash_index": "较适宜",
            "travel_index": "适宜",
            "exercise_index": "适宜",
            "drying_index": ""
        },
        "future": [
            {
                "temperature": "2℃~20℃",
                "weather": "",
                "weather_id": {
                    "fa": "00",
                    "fb": "00"
                },
                "wind": "持续无风向微风",
                "week": "星期一",
                "date": "20230313"
            },
            {
                "temperature": "9℃~25℃",
                "weather": "多云",
                "weather_id": {
                    "fa": "01",
                    "fb": "01"
                },
                "wind": "南风3-5级",
                "week": "星期二",
                "date": "20230314"
            },
            {
                "temperature": "11℃~24℃",
                "weather": "多云转阴",
                "weather_id": {
                    "fa": "01",
                    "fb": "02"
                },
                "wind": "东北风3-5级",
                "week": "星期三",
                "date": "20230315"
            },
            {
                "temperature": "8℃~21℃",
                "weather": "小雨转中雨",
                "weather_id": {
                    "fa": "07",
                    "fb": "08"
                },
                "wind": "持续无风向微风",
                "week": "星期四",
                "date": "20230316"
            },
            {
                "temperature": "7℃~10℃",
                "weather": "小雨转阴",
                "weather_id": {
                    "fa": "07",
                    "fb": "02"
                },
                "wind": "北风3-5级",
                "week": "星期五",
                "date": "20230317"
            },
            {
                "temperature": "8℃~21℃",
                "weather": "小雨转中雨",
                "weather_id": {
                    "fa": "07",
                    "fb": "08"
                },
                "wind": "持续无风向微风",
                "week": "星期六",
                "date": "20230318"
            },
            {
                "temperature": "8℃~21℃",
                "weather": "小雨转中雨",
                "weather_id": {
                    "fa": "07",
                    "fb": "08"
                },
                "wind": "持续无风向微风",
                "week": "星期日",
                "date": "20230319"
            }
        ]
    },
    "error_code": 0
}

(2)、Java代码调用接口

public class WeatherTest {
    public static void main(String[] args) {
        HttpClient1 httpClient = new HttpClient1();
        JSONObject jsonObject = httpClient.getJSON("http://v.juhe.cn/weather/geo?format=2&key=KEY&lon=LON&lat=LAT".replace("KEY", "保密").replace("LON", "114.31667").replace("LAT", "30.51667"));
        WeatherVo weatherVo = jsonObject.getJSONObject("result").getJSONObject("today").toJavaObject(WeatherVo.class);
        System.out.println(weatherVo);
    }
}

工具类:

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Map;

public class HttpClient1 {

    private ConnectionConfig connectionConfig = ConnectionConfig.custom().setBufferSize(4128).build();
    private CloseableHttpClient httpClient = HttpClients.custom().setDefaultConnectionConfig(connectionConfig).build();

    /**
     * get请求-返回JSONObject
     */
    public JSONObject getJSON(String url) {
        try {
            RequestBuilder builder = RequestBuilder.get(url);
            HttpUriRequest request = builder.build();
            return httpClient.execute(request,getResponseHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * post请求-返回JSONObject
     */
    public JSONObject postJSON(String url, Map<String, Object> params) {
        String json=null;
        if(params!=null && params.size() > 0) json= JSON.toJSONString(params);
        return postJSON(url, json);
    }

    /**
     * post请求-返回JSONObject
     */
    public JSONObject postJSON(String url, String json) {
        try {
            HttpPost httpPost = new HttpPost(url);
            if(StringUtils.isNotBlank(json)){
                StringEntity entity = new StringEntity(json,"utf-8");//解决中文乱码问题
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                httpPost.setEntity(entity);
            }
            return httpClient.execute(httpPost,getResponseHandler());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 指定响应返回类型=JSONObject
     */
    private ResponseHandler<JSONObject> getResponseHandler() {
        ResponseHandler<JSONObject> handler = new ResponseHandler<JSONObject>() {
            public JSONObject handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    String json = EntityUtils.toString(entity,"UTF-8");
                    return JSON.parseObject(json);
                }
                return null;
            }
        };
        return handler;
    }
}

封装数据的实体类

@Data
public class WeatherVo implements Serializable {
    /*今日温度*/
    private String temperature;
    /*今日天气*/
    private String weather;
    private String wind;
    private String week;
    private String city;

    @JsonProperty(value = "date_y")
    private String dateY;
    /*穿衣指数*/
    @JsonProperty(value = "dressing_index")
    private String dressingIndex;
    /*穿衣建议*/
    @JsonProperty(value = "dressing_advice")
    private String dressingAdvice;
    /*紫外线强度*/
    @JsonProperty(value = "uv_index")
    private String uvIndex;
    /*舒适度指数*/
    @JsonProperty(value = "comfort_index")
    private String comfortIndex;
    /*洗车指数*/
    @JsonProperty(value = "washIndex")
    private String wash_index;
    /*旅游指数*/
    @JsonProperty(value = "travelIndex")
    private String travel_index;
    /*晨练指数*/
    @JsonProperty(value = "exercise_index")
    private String exerciseIndex;
    /*干燥指数*/
    @JsonProperty(value = "drying_index")
    private String dryingIndex;

}

返回数据:

WeatherVo(temperature=2℃~20℃, weather=晴, wind=持续无风向微风, week=星期一, city=武汉, dateY=2023年03月13日, dressingIndex=较舒适, 
dressingAdvice=建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。, uvIndex=中等, comfortIndex=, wash_index=较适宜,
travel_index=适宜, exerciseIndex=适宜, dryingIndex=)

 

posted on 2023-03-13 18:07  周文豪  阅读(594)  评论(0编辑  收藏  举报