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

fty8788 2011-07-14 03:32:38
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);
}
...全文
1654 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fty8788 2011-07-14
  • 打赏
  • 举报
回复
服务器应该也是utf-8的
IOS版本使用的也是utf-8

改成GBK也没用
xinqiqi123 2011-07-14
  • 打赏
  • 举报
回复
服务器是这么接收的?和服务器编码要一致
qiying1988 2011-07-14
  • 打赏
  • 举报
回复
用GBK 试试

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧