根据城市名称得到当地天气情况
使用的是高德地图的天气查询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 = "="+city+"&key="+KEY_;String s1 = doGet(s);return s1;}@Testpublic void test5(){System.out.println(getWeatherinfo("成都市"));}
根据城市名称得到当地天气情况
使用的是高德地图的天气查询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 = "="+city+"&key="+KEY_;String s1 = doGet(s);return s1;}@Testpublic void test5(){System.out.println(getWeatherinfo("成都市"));}