栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

20220626-JAVA高德地图天气API调用总结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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.com
Apply for Key

Document Download
https://lbs.amap.com/api/webservice/download

#部分数据
厦门市	350200	0592
厦门市市辖区	350201	0592
思明区	350203	0592
海沧区	350205	0592
湖里区	350206	0592
集美区	350211	0592
同安区	350212	0592
翔安区	350213	0592
API format
#返回预报天气
https://restapi.amap.com/v3/weather/weatherInfo?city=>&key=<用户key>&extensions=all
#返回实况天气
https://restapi.amap.com/v3/weather/weatherInfo?city=>&key=<用户key>&extensions=base
Json Entities Class
@Data
public class WeatherDTO {
    private String status;
    private String count;
    private String info;
    private String infocode;
    private ArrayList forecasts;
}


@Data
public class Forecast {
   private String city;
   private String adcode;
   private String province;
   private String reporttime;
   private ArrayList 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 = "***************";

public static WeatherDTO getWeatherObj(String adCode) throws IOException {
        // OkHtppUtil ou1 = new OkHtppUtil();
        //目前的日期
//        湖里区	350206	0592
//        集美区	350211	0592
//        同安区	350212	0592
        String response3 = OU.getMethod("https://restapi.amap.com/v3/weather/weatherInfo?city=" + adCode + "&key=" + AMAP_KEY + "&extensions=all");
        return JSON.parseObject(response3, WeatherDTO.class);
    }



 
    public static String getWeather(String adCode) {
        try {
            WeatherDTO weatherDTO = getWeatherObj(adCode);
            ArrayList forecasts = weatherDTO.getForecasts();
            Forecast one = forecasts.get(0);
            ArrayList casts = one.getCasts();
            String content = "";
            if (casts == null) {
                content = "上方云层异常,疯狂布朗运动ing,暂无天气预报更新!————404";
                return content;
            }
            content = "地区:" + one.getCity() + ",更新时间:" + one.getReporttime() + "
"; for (Cast cast : casts) { content += cast.getDate() + "————天气:" + cast.getDayweather() + ";气温:" + cast.getNighttemp() + "~" + cast.getDaytemp() + "℃;风力:" + cast.getDaypower() + "级" + cast.getDaywind() + "风
"; } 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.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号