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工作流
- 乒乓卡通3d人物
- 一种长着彩虹翅膀的虫子comfyui工作流
- 一只由水晶制成的蜂鸟
- 一只千纸鹤坐在路上哭泣ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!