高德天气接口使用说明
第一步,申请”web服务 API”密钥(Key);
第二步,拼接HTTP请求URL,第一步申请的Key需作为必填参数一同发送;
第三步,接收HTTP请求返回的数据(JSON或XML格式),解析数据。
如无特殊声明,接口的输入参数和输出数据编码全部统一为UTF-8。
返回结果参数
风力表
package lj.service.environment;import java.util.ArrayList;import java.util.Date;import java.util.List;import mons.lang3.time.DateUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.google.gson.JsonArray;import com.google.gson.JsonElement;import com.google.gson.JsonObject;import com.google.gson.JsonParser;import lj.dao.environment.IEnvironmentCheckDao;import lj.global.AppConst;import lj.model.environment.EnvironmentCheck;import lj.util.HttpUtils;import lj.util.StringUtils;@Servicepublic class TemperatureState {@Autowiredprivate IEnvironmentCheckDao environmentCheckDao = null;//private String appId = "92631425";//private String appSecret = "2vFPWIx2";//使用高德apiprivate String gaodeKeyNow="略";private String gaodeKeyForcast="略";/*** 使用gaodeKeyNow 使用高德接口 * * @param city 城市名称* @return 天气状态 晴 阴 雨 雪 多云 有风* */public String getTemperatureState(String city) {if (StringUtils.isNullOrEmpty(city)) {return "城市不能为空";}if (city.indexOf("市") == city.length() - 1)city = city.replace("市", "");if (city.indexOf("县") == city.length() - 1)city = city.replace("县", "");//获取城市IDString cityid = getCityCode(city);String url = String.format("/v3/weather/weatherInfo?extensions=base&key=%s&city=%s", gaodeKeyNow,cityid);// 获取天气信息String res = HttpUtils.doGetRest(url);// 解析天气信息中的天气状况JsonParser parser = new JsonParser();JsonElement element = parser.parse(res);JsonObject root = element.getAsJsonObject();JsonArray array = root.get("lives").getAsJsonArray();if(root.get("status").getAsString()=="0" || array.size() == 0) {return AppConst.WEATHER_DEFAULT;}JsonElement obj = array.get(0);JsonObject obj1 = obj.getAsJsonObject();String temperatureState = obj1.get("weather").getAsString();return judgeState(temperatureState);}//返回天气状态private static String judgeState(String state) {if (StringUtils.isNullOrEmpty(state)) {return AppConst.WEATHER_DEFAULT;}if (state.equals("晴")) {return AppConst.WEATHER_SUN;} else if (state.equals("阴")) {return AppConst.WEATHER_DARK;} else if (AppConst.WEATHER_RAINSET.contains(state)) {return AppConst.WEATHER_RAIN;} else if (AppConst.WEATHER_SNOWSET.contains(state)) {return AppConst.WEATHER_SNOW;} else if (AppConst.WEATHER_CLOUDSET.contains(state)) {return AppConst.WEATHER_CLOUD;} else {return AppConst.WEATHER_DEFAULT;}}/*** 返回今、明、后三天天气* * @return*/public List<EnvironmentCheck> findNearestThreeDays(String city) {if (StringUtils.isNullOrEmpty(city)) {return null;}if (city.indexOf("市") == city.length() - 1)city = city.replace("市", "");if (city.indexOf("县") == city.length() - 1)city = city.replace("县", "");List<EnvironmentCheck> list = new ArrayList<EnvironmentCheck>();//获取城市IDString cityid = getCityCode(city);String url = String.format("/v3/weather/weatherInfo?extensions=all&key=%s&city=%s", gaodeKeyForcast,cityid);// 获取天气信息String res = HttpUtils.doGetRest(url);// 解析天气信息中的天气状况JsonParser parser = new JsonParser();JsonElement element = parser.parse(res);JsonObject root = element.getAsJsonObject();JsonArray forecasts = root.get("forecasts").getAsJsonArray();JsonElement fore = forecasts.get(0);JsonObject forcast = fore.getAsJsonObject();JsonArray casts = forcast.get("casts").getAsJsonArray();Date checkTime = new Date();try {checkTime=this.environmentCheckDao.getNowTime();}catch(Exception ex){checkTime=new Date();}for (int i = 0; i < 3; i++) {JsonElement obj = casts.get(i);JsonObject obj1 = obj.getAsJsonObject();//温度String temstr = obj1.get("daytemp").getAsString();//风力,比如4 <3String speed1 = obj1.get("daypower").getAsString(); //天气String weather = obj1.get("dayweather").getAsString();list.add(new EnvironmentCheck(checkTime,Double.valueOf(temstr), judgeSpeed(speed1), weather));checkTime=DateUtils.addDays(checkTime, 1);}return list;}// 根据等级转风速m/s ,private double judgeSpeed(String speed1) {boolean isDig = Character.isDigit(speed1.charAt(0));// 第一位是不是数字if (speed1.charAt(0) == 48) {return 0.1;} else if (speed1.charAt(0) == 49 || (!isDig && speed1.charAt(1) == 49)) {// 1或者<1 ASCI 49 return 0.9;} else if (speed1.charAt(0) == 50 || (!isDig && speed1.charAt(1) == 50)) {return 2.45;} else if (speed1.charAt(0) == 51 || (!isDig && speed1.charAt(1) == 51)) {return 4.4;} else if (speed1.charAt(0) == 52 || (!isDig && speed1.charAt(1) == 52)) {return 6.7;} else if (speed1.charAt(0) == 53 || (!isDig && speed1.charAt(1) == 53)) {return 9.35;} else if (speed1.charAt(0) == 54 || (!isDig && speed1.charAt(1) == 54)) {return 12.05;} else if (speed1.charAt(0) == 55 || (!isDig && speed1.charAt(1) == 55)) {return 15.5;} else if (speed1.charAt(0) == 56 || (!isDig && speed1.charAt(1) == 56)) {return 18.95;} else if (speed1.charAt(0) == 57 || (!isDig && speed1.charAt(1) == 57)) {return 22.6;} else if ((speed1.charAt(0) == 49 && speed1.charAt(1) == 48) || (!isDig && speed1.charAt(0) == 49 && speed1.charAt(1) == 48)) {// 10return 26.45;} else if ((speed1.charAt(0) == 49 && speed1.charAt(1) == 49) || (!isDig && speed1.charAt(0) == 49 && speed1.charAt(1) == 49)) {// 11return 30.55;} else if ((speed1.charAt(0) == 49 && speed1.charAt(1) == 50) || (!isDig && speed1.charAt(0) == 49 && speed1.charAt(1) == 50)) {// 12return 32.6;} else {return 0.0;}}/*** * @param city 城市名称* @return 城市编码*/private String getCityCode(String city) {if(city.equals("江阴") == true) {return "320281";}else if(city.equals("靖江") == true) {return "321282";}String url = String.format("/v3/config/district?key=%s&keywords=%s&subdistrict=0&extensions=base", gaodeKeyNow,city);String res = HttpUtils.doGetRest(url);// 解析天气信息中的天气状况JsonParser parser = new JsonParser();JsonElement element = parser.parse(res);JsonObject root = element.getAsJsonObject();JsonArray array = root.get("districts").getAsJsonArray();//如果城市获取不到,或者城市名称不存在,默认使用江阴if(root.get("status").getAsString()=="0" || array.size() == 0) {return "320281";}JsonElement obj = array.get(0);JsonObject obj1 = obj.getAsJsonObject();String cityCode = obj1.get("adcode").getAsString();return cityCode;}/**public String getTemperatureState(String city) {if (StringUtils.isNullOrEmpty(city)) {return "城市不能为空";}String url = String.format("/api?version=v6&appid=%s&appsecret=%s&city=%s", appId,appSecret, city);// 获取天气信息String res = HttpUtils.doGetRest(url);System.out.println(res);// 解析天气信息中的天气状况JsonParser parser = new JsonParser();JsonElement element = parser.parse(res);JsonObject root = element.getAsJsonObject();String temperatureState = root.get("wea").getAsString();return judgeState(temperatureState);}*/}
获取今明后三天天气部分步骤:
URL–> String res --> json element --> json对象 --> 获取某个"元素" 转成Array --> 数组[0] 转成 json element --> json对象 --> 获取对象中的某个数组元素 转成jsonArray -->[0] 转element --> 转对象 -->获取对象中元素get" "
请求结果
{"status":"1","count":"1","info":"OK","infocode":"10000","forecasts":[{"city":"朝阳区","adcode":"110105","province":"北京","reporttime":"-03-04 15:29:57","casts":[{"date":"-03-04","week":"4","dayweather":"多云","nightweather":"多云","daytemp":"11","nighttemp":"3","daywind":"西南","nightwind":"西南","daypower":"≤3","nightpower":"≤3"},{"date":"-03-05","week":"5","dayweather":"小雨","nightweather":"阴","daytemp":"10","nighttemp":"1","daywind":"东","nightwind":"东","daypower":"4","nightpower":"4"},{"date":"-03-06","week":"6","dayweather":"多云","nightweather":"晴","daytemp":"5","nighttemp":"-4","daywind":"南","nightwind":"南","daypower":"≤3","nightpower":"≤3"},{"date":"-03-07","week":"7","dayweather":"晴","nightweather":"多云","daytemp":"8","nighttemp":"-1","daywind":"西南","nightwind":"西南","daypower":"≤3","nightpower":"≤3"}]}]}