OutputStreamWriter用UTF-8编码发中文字符的问题

时间:2022-10-31 07:41:06
OutputStreamWriter往WebService发中文字节,服务器收到的却是乱码
字符串已经转为UTF-8了,各位看看问题出在哪
能加UTF-8的地方都加上了:
OutputStreamWriter out = new OutputStreamWriter(con
     .getOutputStream(),"UTF-8");
out.write(new String(xmlInfo.getBytes("utf-8"),"UTF-8"));

代码:
final String SERVER_URL = "http://123.138.18.11/Hi8WebService/Hi8WebService.asmx"; // 定义需要获取的内容来源地址

URL url = new URL(SERVER_URL);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Pragma:", "no-cache");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "text/xml");

OutputStreamWriter out = new OutputStreamWriter(con
     .getOutputStream(),"UTF-8");
            
String xmlInfo = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
        +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
        +"<soap:Body>\n"
        +"<InsertGroup xmlns=\"http://hi.400pbx.com/\">\n"
        +"<Hi8ID>"+ User.usersid.toString() +"</Hi8ID>\n"
        +"<Hi8PWD>" + User.password +"</Hi8PWD>\n"
        +"<GroupName>" + group.name + "</GroupName>"
        +"</InsertGroup>\n"
        +"</soap:Body>\n"
        +"</soap:Envelope>\n";
Toast.makeText(ctx, group.name, Toast.LENGTH_LONG).show();
// 发送
out.write(new String(xmlInfo.getBytes("utf-8"),"UTF-8"));
        out.flush();
out.close();
// 取返回值
BufferedReader br = new BufferedReader(new InputStreamReader(con
        .getInputStream()));
StringBuilder sBuilder = new StringBuilder();
String line = "";
for (line = br.readLine(); line != null; line = br.readLine()) {
        sBuilder.append(line);
}            

3 个解决方案

#1


用GBK 试试

#2


服务器是这么接收的?和服务器编码要一致

#3


服务器应该也是utf-8的
IOS版本使用的也是utf-8

改成GBK也没用

#1


用GBK 试试

#2


服务器是这么接收的?和服务器编码要一致

#3


服务器应该也是utf-8的
IOS版本使用的也是utf-8

改成GBK也没用