现在的位置: 首页 > 综合 > 正文

Android Socket 中文乱码彻底解决

2018年05月11日 ⁄ 综合 ⁄ 共 362字 ⁄ 字号 评论关闭


主要还是服务器端和客户端编码匹配的问题

不管用何种编码

只要两端匹配

就ok

不要假定默认编码

一个个去试

不如一行code


关键代码

public static final String bm="GBK"; //全局定义,以适应系统其他部分

InputStream is;
BufferedReader br;

bw = new BufferedWriter(new OutputStreamWriter(os,bm));
br = new BufferedReader(new InputStreamReader(is,bm));

bw.write(msg);


Msg = new String(b,bm);


OutputStream os = socket.getOutputStream();
os.write(str.getBytes(bm));


整个工程中和编码相关的部分如上所示


效果如下










抱歉!评论已关闭.