public class WeatherUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(WeatherUtil.class);
public static String doGet(String path){
StringBuilder sb = new StringBuilder();
InputStream is = null;
BufferedReader br = null;
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(1000);
conn.setConnectTimeout(1000);
conn.setRequestProperty("accept" , "*/*");
conn.setRequestProperty("contentType","application/json;charset=utf-8");
is = new GZIPInputStream(conn.getInputStream());
br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
String line;
while ((line = br.readLine())!=null){
sb.append(line);
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (br!=null){
br.close();
}
if (is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
LOGGER.info("天气={}",sb);
return sb.toString();
}
}