索爱GB2312编码 有乱码如何解决?
我在索爱w850i上遇到的问题,通过之前的测试,我已经知道它不支持GBK编码。
我从服务器上读取一个文本文件,我之前用字节流读取,读完后用 new String(chunk,"GB2312")还原,但是会有某些字符是乱码,我换成字节流读取就完全没有问题,这是为什么呢?
一下是两种读取方法的代码。
字符流读取方法
Java code
public void connection1() {
new Thread(new Runnable() {
public void run() {
HttpConnection httpConn = null;
InputStreamReader is = null;
try {
httpConn = (HttpConnection) Connector.open(rul);
int code = httpConn.getResponseCode();
char[] data = new char[1024 ];
if (code == HttpConnection.HTTP_OK) {
// 获得输入流 接受数据
is = new InputStreamReader(httpConn.openInputStream(),"gb2312");
int ch = 0;
while ((ch = is.read(data)) != -1) {
char[] chunk = new char[ch];
System.arraycopy(data, 0, chunk, 0, ch);
sb.append(data);//sb StringBuffer类型的一个全局变量用于显示在屏幕上
}
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
字节流读取方法
Java code public void connection1() {
new Thread(new Runnable() {
public void run() {
HttpConnection httpConn = null;
InputStream is = null;
try {
httpConn = (HttpConnection) Connector
.open(url);
int code = httpConn.getResponseCode();
byte[] data = new byte[1024 ];
if (code == HttpConnection.HTTP_OK) {
// 获得输入流 接受数据
is = httpConn.openInputStream();
int ch = 0;
while ((ch = is.read(data)) != -1) {
byte[] chunk = new byte[ch];
System.arraycopy(data, 0, chunk, 0, ch);
String s = new String(chunk,"GB2312");
sb.append(data);
}
}
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}