码迷,mamicode.com
首页 > 其他好文 > 详细

获取天气信息,解决乱码问题

时间:2021-05-24 14:15:35      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:解决乱码   乱码   cti   color   zip   ack   builder   content   request   

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();
    }
}

 

获取天气信息,解决乱码问题

标签:解决乱码   乱码   cti   color   zip   ack   builder   content   request   

原文地址:https://www.cnblogs.com/padazala/p/14779210.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!