Java获取天气信息
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public static JSONObject getWeatherInfo(String adcode, String key) throws Exception { String url = "https://restapi.amap.com/v3/weather/weatherInfo"; String requestUrl = url + "?key=" + key + "&city=" + adcode; URL obj = new URL(requestUrl); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject result = JSONObject.parseObject(response.toString()); return result; }
Java解析并显示天气信息
public static void main(String[] args) { try { String adcode = "城市编码"; // 例如:110000(北京市) String key = "你的开发者Key"; JSONObject weatherInfo = getWeatherInfo(adcode, key); JSONArray forecasts = weatherInfo.getJSONArray("forecasts"); JSONObject todayForecast = forecasts.getJSONObject(0); JSONArray casts = todayForecast.getJSONArray("casts"); for (int i = 0; i < casts.size(); i++) { JSONObject cast = casts.getJSONObject(i); String date = cast.getString("date"); String week = cast.getString("week"); String dayWeather = cast.getString("dayweather"); String nightWeather = cast.getString("nightweather"); System.out.println(date + " " + week + " " + dayWeather + "转" + nightWeather); } } catch (Exception e) { e.printStackTrace(); } }
Java代码如何利用高德地图API实现行政区划查询的热门城市推荐
标签: Java开发, Java显示天气信息, 高德地图api
上面是“Java开发如何利用高德地图api解析并显示天气信息”的全面内容,想了解更多关于 后端开发 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_12312.html
workflows工作流
迷人的月光,发光的花朵
一个红头发明亮眼睛的漂亮女人
羚羊安上小白兔的门牙ComfyUI工作流
一支令人难忘的狙击步枪ComfyUI工作流
树上挂着一只快乐的小樱桃
一棵白色心形流苏树comfyui工作流
一匹热血沸腾的汗血宝马庄严地站立着ComfyUI工作流
1个粉红色头发可爱的女孩ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!