20220626-JAVA高德地图天气API调用总结
生活随笔
收集整理的這篇文章主要介紹了
20220626-JAVA高德地图天气API调用总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AutoNavi——Weather API Usage
Background
I need information of weather forecast ,so I choose AutoNavi API as it is widely used in China
Website
https://lbs.amap.comApply for Key
Document Download
https://lbs.amap.com/api/webservice/download#部分數據<adcode> 廈門市 350200 0592 廈門市市轄區 350201 0592 思明區 350203 0592 海滄區 350205 0592 湖里區 350206 0592 集美區 350211 0592 同安區 350212 0592 翔安區 350213 0592API format
#返回預報天氣 https://restapi.amap.com/v3/weather/weatherInfo?city=<adcode>&key=<用戶key>&extensions=all #返回實況天氣 https://restapi.amap.com/v3/weather/weatherInfo?city=<adcode>&key=<用戶key>&extensions=baseJson Entities Class
@Data public class WeatherDTO {private String status;private String count;private String info;private String infocode;private ArrayList<Forecast> forecasts; }@Data public class Forecast {private String city;private String adcode;private String province;private String reporttime;private ArrayList<Cast> casts; }@Data public class Cast {private String date;private String week;private String dayweather;private String nightweather;private String daytemp;private String nighttemp;private String daywind;private String nightwind;private String daypower;private String nightpower; }Code Reference
private static final OkHtppUtil OU = new OkHtppUtil();private static final String AMAP_KEY = "***************"; /*** 區號** @param adCode* @return* @throws IOException*/ public static WeatherDTO getWeatherObj(String adCode) throws IOException {// OkHtppUtil ou1 = new OkHtppUtil();//目前的日期 // 湖里區 350206 0592 // 集美區 350211 0592 // 同安區 350212 0592String response3 = OU.getMethod("https://restapi.amap.com/v3/weather/weatherInfo?city=" + adCode + "&key=" + AMAP_KEY + "&extensions=all");return JSON.parseObject(response3, WeatherDTO.class);}/*** 將json對象轉為可讀的html文本,+br處理** @param adCode* @return* @throws IOException*/public static String getWeather(String adCode) {try {WeatherDTO weatherDTO = getWeatherObj(adCode);ArrayList<Forecast> forecasts = weatherDTO.getForecasts();Forecast one = forecasts.get(0);ArrayList<Cast> casts = one.getCasts();String content = "";if (casts == null) {content = "上方云層異常,瘋狂布朗運動ing,暫無天氣預報更新!————404";return content;}content = "地區:<b>" + one.getCity() + "</b>,更新時間:" + one.getReporttime() + "<br>";for (Cast cast : casts) {content += cast.getDate() + "————天氣:" + cast.getDayweather() + ";氣溫:" + cast.getNighttemp() + "~" + cast.getDaytemp() + "℃;風力:" + cast.getDaypower() + "級" + cast.getDaywind() + "風<br>";}return content;} catch (IOException e) {return "上方云層異常,瘋狂布朗運動ing,暫無天氣預報更新!————403";}}Finally
It is no exaggeration to say that it’s easy to use. There are many other APIs in AutoNavi, hope I could use some of them in the future if possible.
總結
以上是生活随笔為你收集整理的20220626-JAVA高德地图天气API调用总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mybatis插入图片处理--mysql
- 下一篇: java jre 中导入导出证书