今天遇到这个问题, 直接请求后台给的接口得到乱码,使用文本输出流+“编码格式” 后,可以正常获得值。
String spec = "http://www.7723.cn/tj/is_down/?is_down=0";
URL url = new URL(spec);HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5 * 1000);
conn.setReadTimeout(5 * 1000);
conn.setUseCaches(true);
conn.setRequestMethod("GET");
conn.connect();
InputStream inputStream = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "GBK"));
String line = null;
StringBuffer sb = new StringBuffer();
while ((line = br.readLine()) != null) {
sb.append(line);
}
String result = sb.toString();