原方法:
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes(), "utf-8");
sb.append(lines);
在new InputStreamReader时默认用文件的编码格式(gbk)解析,若字符串中汉字个数为单数,此时最后一位汉字会因为少1个字节导致乱码;之后的 new String(lines.getBytes(), "utf-8")操作解析不出最后一位
解决方法:
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String lines;
StringBuffer sb = new StringBuffer("");
while ((lines = reader.readLine()) != null) {
lines = new String(lines.getBytes());
sb.append(lines);
}
读取字符流时直接使用utf-8,会直接接受并转化为bgk格式,不会导致乱码;