200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > java 获取【和风天气】的天气信息

java 获取【和风天气】的天气信息

时间:2023-12-03 05:32:10

相关推荐

java 获取【和风天气】的天气信息

和风天气地址:和风天气 | 商业气象服务商, 天气预报,灾害预警,台风路径,卫星云图,天气API/SDK/APP, 天气插件, 历史天气, 气象可视化

需要登录和风天气注册自己的api_key,在用key的情况下才能调用

提供了根据体感温度获取体感等级的方法。

package org.jeecg.utils;import com.alibaba.fastjson.JSONObject;import lombok.extern.slf4j.Slf4j;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import .URL;import .URLConnection;import java.util.zip.GZIPInputStream;@Slf4jpublic class WeatherUtil {/*** 从和风天气获取天气信息** @param location 城市编码* @param keyAPI Key* @return*/public static String getWeatherByHFTQ(String location, String key) {StringBuffer sb = new StringBuffer();try {String weather_url ="/v7/weather/now?location=" + location + "&key=" + key;URL url = new URL(weather_url);URLConnection conn = url.openConnection();InputStream is = conn.getInputStream();GZIPInputStream gzin = new GZIPInputStream(is);InputStreamReader isr = new InputStreamReader(gzin, "utf-8"); // 设置读取流的编码格式,自定义编码BufferedReader reader = new BufferedReader(isr);String line = null;while ((line = reader.readLine()) != null)sb.append(line + " ");reader.close();// 解析JSON格式的天气信息数据JSONObject json = JSONObject.parseObject(sb.toString());JSONObject now = json.getJSONObject("now");String obsTime = now.getString("obsTime");//数据观测时间String temp = now.getString("temp"); // 获取温度String feelsLike = now.getString("feelsLike"); //体感温度String feelsLikeLevel = getFeelsLikeLevel(new Double(feelsLike));String text = now.getString("text"); //天气状况的文字描述,包括阴晴雨雪等天气状态的描述String windDir = now.getString("windDir"); // 获取风向String windSpeed = now.getString("windSpeed"); // 获取风速,公里/小时String windScale = now.getString("windScale"); // 获取风力String humidity = now.getString("humidity"); // 获取湿度String pressure = now.getString("pressure"); // 大气压强,默认单位:百帕// 输出获取到的天气信息System.out.println("城市编号:" + location);System.out.println("数据观测时间:" + obsTime);System.out.println("天气状况的文字描述:" + text);System.out.println("温度:" + temp + "℃");System.out.println("体感温度:" + feelsLike + "℃");System.out.println("体感等级:" + feelsLikeLevel);System.out.println("湿度:" + humidity + "%");System.out.println("风向:" + windDir);System.out.println("风速:" + windSpeed + "公里/小时");System.out.println("风力:" + windScale + "级");System.out.println("气压:" + pressure + "百帕");} catch (IOException e) {e.printStackTrace();System.out.println("获取天气信息失败:" + e);}return sb.toString();}/*** 根据体感温度获取体感等级** @param temp 体感温度* @return 体感等级*/public static String getFeelsLikeLevel(double temp) {String level;if (temp > 40) {level = "极热";} else if (temp > 35) {level = "酷热";} else if (temp > 30) {level = "炎热";} else if (temp > 27) {level = "温暖";} else if (temp > 20) {level = "适宜";} else if (temp > 15) {level = "有点凉";} else if (temp > 10) {level = "凉爽";} else if (temp > 5) {level = "微冷";} else {level = "寒冷";}return level;}public static void main(String[] args) {String location = "101010100"; // 设置要查询的城市String key = "your_api_key"; // 替换成你的API KeyString weather = getWeatherByHFTQ(location, key);System.out.println(weather);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。