This commit is contained in:
Kane Wang 2022-03-16 16:05:17 +08:00
parent f2c340d3cb
commit 0e574c7031
1 changed files with 6 additions and 2 deletions

View File

@ -16,14 +16,17 @@ public class WeatherDisasterWarningGrabber
private static String QUERY_URL = "https://devapi.qweather.com/v7/warning/now?"; private static String QUERY_URL = "https://devapi.qweather.com/v7/warning/now?";
private static String USER_KEY = "fe9fa8eeeb6f4301a92541eed565dd15"; private static String USER_KEY = "fe9fa8eeeb6f4301a92541eed565dd15";
/***
* 从和风天气获取天气警报json字符串
* @param cityCode 城市或区域代码
* @return 返回警报的json字符串
*/
public static String getWeatherDisasterWarningJSON( String cityCode ) public static String getWeatherDisasterWarningJSON( String cityCode )
{ {
//拼接url字符串 //拼接url字符串
String json = ""; String json = "";
String requestURL = QUERY_URL + "key=" + USER_KEY + "&location=" + cityCode; String requestURL = QUERY_URL + "key=" + USER_KEY + "&location=" + cityCode;
//requestURL = "http://127.0.0.1:8000/ajax";
//链接用 //链接用
HttpURLConnection connection = null; HttpURLConnection connection = null;
URL url = null; URL url = null;
@ -46,6 +49,7 @@ public class WeatherDisasterWarningGrabber
//如果responseCode为200说明访问成功 //如果responseCode为200说明访问成功
if ( connection.getResponseCode() == 200 ) if ( connection.getResponseCode() == 200 )
{ {
//注意和风使用了gizp压缩响应体
inputStream = new GZIPInputStream( connection.getInputStream() ); inputStream = new GZIPInputStream( connection.getInputStream() );
bufferedReader = new BufferedReader( new InputStreamReader( inputStream, "UTF-8" ) ); bufferedReader = new BufferedReader( new InputStreamReader( inputStream, "UTF-8" ) );