使用的是高德地图的天气查询api
private static final String KEY_ = "1aaa6aaa97b964b57fcc13176b307c5c";
public static String doGet(String url) {
//创建一个Http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
//创建一个get请求
HttpGet httpGet = new HttpGet(url);
//响应模型
CloseableHttpResponse response = null;
try {
//由客户端发送get请求
response = httpClient.execute(httpGet);
//从响应模型中获取响应实体
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
return EntityUtils.toString(responseEntity);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
//根据城市名称,查询城市天气
public String getWeatherinfo(String city){
String s = "https://restapi.amap.com/v3/weather/weatherInfo?city="+city+"&key="+KEY_;
String s1 = doGet(s);
return s1;
}
@Test
public void test5(){
System.out.println(getWeatherinfo("成都市"));
}