调用天气预报以及接口出现乱码解决方法
2017-11-29 20:40
1056 查看
[转载:天气预报接口(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=101010100 xml数据: 通过城市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); }
相关文章推荐
- Java中容易出现乱码的地方以及解决方法
- Java中容易出现乱码的地方以及解决方法
- php 写入数据到MySQL以及从MySQL获取数据,页面出现乱码的解决方法
- 调用ffmpeg库出现的问题以及解决方法
- Java中容易出现乱码的地方以及解决方法
- Putty 中调用linux tree命令后出现乱码的解决方法
- ubuntu 下MySQL出现乱码的解决方法以及如何设置编码方式
- 在C#中调用Matlab函数出现的问题以及解决方法
- android上传接口(有汉字)出现乱码解决方法
- python用requests模块时,调用text方法出现中文乱码的解决办法
- LR11录制回放出现中文乱码以及录制时一直跳到360浏览器的解决方法
- Java中容易出现乱码的地方以及解决方法
- 关于调用listFile()在遍历含有乱码文件时出现input is not valid Modified UTF-8错误的解决方法
- "调用扫描接口出现异常”解决方法
- QT5关于中文乱码,以及出现C2001错误解决方法
- js调用出现乱码的解决方法探索+js行距
- Java中容易出现乱码的地方以及解决方法
- linux下vim安装以及编辑文件出现乱码解决方法
- LR11录制回放出现中文乱码以及录制时一直跳到360浏览器的解决方法
- NuSOAP 调用 Web Service 出现乱码的解决方法: