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

20220626-JAVA高德地图天气API调用总结_第1张图片

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<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	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);
    }



 /**
     * 将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 = "地区:" + 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.

20220626-JAVA高德地图天气API调用总结_第2张图片

你可能感兴趣的:(JavaSE,java,json,前端)