请求天气接口(http://wthrcdn.etouch.cn/weather_mini?citykey=101210101) 乱码问题
public static String sendGet2(String url) throws IOException {
//实例化httpclient,(4.5新版本和以前不同),实例化方式有两种
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0");
CloseableHttpResponse response = null;
try {
/**
* 底层http链接仍由响应对象保存
* 允许直接从网络套接字流式传输响应内容
* 为了确保正确释放系统资源
* 用户必须从finally子句中调用CloseableHttpResponse #close()
*/
response = httpClient.execute(httpGet);
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
//对响应主体做一些有用的事情
//并确保他完全被消耗掉
String s = EntityUtils.toString(entity, "utf-8");
return s;
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
response.close();
}
}
即可解决该问题。