高德天气API官网地址如下
https://lbs.amap.com/api/webservice/guide/api/weatherinfo
对于个人认证开发者,日调用超量封停(次/日)限制为300000,QPS(次/秒)限制为200,对于企业认证开发者,日调用超量封停(次/日)限制为3000000,QPS(次/秒)限制为1000
代码实例如下:
controller代码
/**
* 天气接口
*
* @author liujy
* @since 2021/11/9 10:35
*/
@RestController
@RequestMapping(value = "/screen/w/weather")
@Slf4j
public class WebScreenWeatherController {
@Autowired
private ScreenWeatherService screenWeatherService;
/**
* 高德天气api
*
* @author liujy
* @since 2021/11/9 10:45
*/
@ApiOperation(value = "天气数据")
@GetMapping("/data")
public JSONObject getWeatherData() {
JSONObject res = this.screenWeatherService.get();
return res;
}
}
service代码
@Service
public class ScreenWeatherService {
@Autowired
private RestTemplate restTemplate;
/**
* 获取要返回的天气信息
*
* @author liujy
* @since 2021/11/9 14:39
*/
public JSONObject get() {
String url = "https://restapi.amap.com/v3/weather/weatherInfo?key=%s&city=%s&extensions=%s&output=%s";
// 开发者key
String key = "your key value";
// 城市编码
String city = "110101";
// 返回天气类型
String extensions = "base";
// 指定返回json
String output = "JSON";
final String formatUrl = String.format(url, key, city, extensions, output);
final String body = restTemplate.getForObject(formatUrl, String.class);
final JSONObject jsonObject = JSONObject.parseObject(body);
final JSONObject data = jsonObject.getJSONArray("lives").getJSONObject(0);
JSONObject res = new JSONObject();
// 天气
final String weather = data.getString("weather");
// 温度
final String temperature = data.getString("temperature");
// 湿度
final String humidity = data.getString("humidity").concat("%");
// 风力
final String windpower = data.getString("windpower").concat("级");
res.put("city", "北京");
res.put("weather", weather);
res.put("temperature", temperature);
res.put("humidity", humidity);
res.put("windpower", windpower);
return res;
}
}
返回结果如下:
{
"status": "1",
"count": "1",
"info": "OK",
"infocode": "10000",
"lives": [
{
"province": "北京",
"city": "东城区",
"adcode": "110101",
"weather": "晴",
"temperature": "8",
"winddirection": "北",
"windpower": "4",
"humidity": "27",
"reporttime": "2021-11-11 09:33:49"
}
]
}