JAVA query local weather, weather api

The free interface provided by the aggregated data used by the weather forecast interface service can be called 100 times per day for free. You can register and activate through https://www.juhe.cn/docs/api/id/73

Interface address: http://apis.juhe.cn/simpleWeather/query
Return format: json
Request method: http get/post
request example: http://apis.juhe.cn/simpleWeather/query?city=%E8%8B %8F%E5%B7%9E&key=
Interface note: Query the weather forecast by city name or city ID

city		string	要查询的城市名称/id,城市名称如:温州、上海、北京,需要utf8 urlencode
key		string	在个人中心->我的数据,接口名称上方查看
name type illustrate
error_code int Return code, 0 means the query is successful
reason string Return instructions
result string Return result set
realtime - Current weather details
info string Weather conditions, such as: sunny, cloudy
wid string Weather identification id, please refer to small interface 2
temperature string Temperature, may be empty
humidity string Humidity, may be empty
direct string Wind direction, may be empty
power string Wind power, possibly empty
aqi string Air quality index, may be empty
future - Weather conditions in the past 5 days
date string date
temperature string Temperature, min/max
weather string weather conditions
direct string wind direction

{
    
    
    "reason": "查询成功",
    "result": {
    
    
        "city": "苏州",
        "realtime": {
    
    
            "temperature": "4",
            "humidity": "82",
            "info": "阴",
            "wid": "02",
            "direct": "西北风",
            "power": "3级",
            "aqi": "80"
        },
        "future": [
            {
    
    
                "date": "2019-02-22",
                "temperature": "1/7℃",
                "weather": "小雨转多云",
                "wid": {
    
    
                    "day": "07",
                    "night": "01"
                },
                "direct": "北风转西北风"
            },
            {
    
    
                "date": "2019-02-23",
                "temperature": "2/11℃",
                "weather": "多云转阴",
                "wid": {
    
    
                    "day": "01",
                    "night": "02"
                },
                "direct": "北风转东北风"
            },
            {
    
    
                "date": "2019-02-24",
                "temperature": "6/12℃",
                "weather": "多云",
                "wid": {
    
    
                    "day": "01",
                    "night": "01"
                },
                "direct": "东北风转北风"
            },
            {
    
    
                "date": "2019-02-25",
                "temperature": "5/12℃",
                "weather": "小雨转多云",
                "wid": {
    
    
                    "day": "07",
                    "night": "01"
                },
                "direct": "东北风"
            },
            {
    
    
                "date": "2019-02-26",
                "temperature": "5/11℃",
                "weather": "多云转小雨",
                "wid": {
    
    
                    "day": "01",
                    "night": "07"
                },
                "direct": "东北风"
            }
        ]
    },
    "error_code": 0
}

For example:

// 连接中央气象台的API
		Object weather = redisTemplate.opsForValue().get("weather");
		if (weather == null) {
    
    
			URL url = null;
			try {
    
    
				url = new URL(
						"http://apis.juhe.cn/simpleWeather/query?city=%E6%9E%A3%E5%BA%84&key=----------------------");
			}
			catch (MalformedURLException e) {
    
    
				e.printStackTrace();
			}
			URLConnection connectionData = null;
			try {
    
    
				connectionData = url.openConnection();
			}
			catch (IOException e) {
    
    
				e.printStackTrace();
			}
			connectionData.setConnectTimeout(1000);
			Map<String, Object> map = new HashMap<String, Object>();
			try {
    
    
				BufferedReader br = new BufferedReader(new InputStreamReader(connectionData.getInputStream(), "UTF-8"));
				StringBuilder sb = new StringBuilder();
				String line = null;
				while ((line = br.readLine()) != null) {
    
    
					sb.append(line);
				}
				String datas = sb.toString();
				JSONObject jsonData = JSON.parseObject(datas);
				String result = jsonData.getString("result");
				HashMap map1 = JSON.parseObject(result, HashMap.class);
				redisTemplate.opsForValue().setIfAbsent("weather", map1, 3600, TimeUnit.SECONDS);
				return map1;

			}
			catch (SocketTimeoutException e) {
    
    
				System.out.println("连接超时");
			}
			catch (FileNotFoundException e) {
    
    
				System.out.println("加载文件出错");
			}
			catch (UnsupportedEncodingException e) {
    
    
				e.printStackTrace();
			}
			catch (IOException e) {
    
    
				e.printStackTrace();
			}
			catch (Exception e) {
    
    
				e.printStackTrace();
			}

			return map;

Guess you like

Origin blog.csdn.net/wang121213145/article/details/132454637