调用天气预报以及接口出现乱码解决方法

来源:互联网 发布:门户源码 编辑:程序博客网 时间:2024/06/06 11:43
[转载:天气预报接口(json数据,xml数据)](http://blog.csdn.net/fancylovejava/article/details/26102635)json数据:通过城市名字获得天气数据,json数据:http://wthrcdn.etouch.cn/weather_mini?city=北京通过城市id获得天气数据,json数据:http://wthrcdn.etouch.cn/weather_mini?citykey=101010100xml数据:通过城市id获得天气数据,xml文件数据,当错误时会有<error>节点http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100通过城市名字获得天气数据,xml文件数据:http://wthrcdn.etouch.cn/WeatherApi?city=北京[转载:调用天气预报接口出现乱码解决方法](http://blog.csdn.net/beibeijia125/article/details/70229221)public static String getWeatherInfo(String url) {         CloseableHttpClient client;         client = HttpClients.createDefault();         HttpGet get = new HttpGet(url);         HttpResponse response;         try {             response = client.execute(get);             HttpEntity entity = response.getEntity();             if (entity != null) {                 InputStream instreams = entity.getContent();                 String str = WeatherUtil.convertStreamToString(instreams);                 get.abort();                 return str;             }         } catch (IOException e) {             e.printStackTrace();         }         return null;     }  private static String convertStreamToString(InputStream is) {         StringBuilder sb1 = new StringBuilder();         byte[] bytes = new byte[4096];         int size;         try {             while ((size = is.read(bytes)) > 0) {                 String str = new String(bytes, 0, size, "UTF-8");                 sb1.append(str);             }         } catch (IOException e) {             e.printStackTrace();         } finally {             try {                 is.close();             } catch (IOException e) {                 e.printStackTrace();             }         }         return sb1.toString();     }     public static void main(String[] args) {           String url = "http://wthrcdn.etouch.cn/weather_mini?city=北京";             String weatherInfo = getWeatherInfo(url);            System.out.println(weatherInfo);       }
原创粉丝点击