您的位置:首页 > 移动开发 > Android开发

android向web提交数据,中文乱码

2014-11-12 10:33 253 查看
============问题描述============

源码如下所示, 这时候“张三”这个字符到web已经是两个“??”,怎么破,查了不少方法,

如URLDecoder.decode(“张三”, "utf-8"),或者"张三".getBytes()都不好用啊,求破

public static String GetXml() throws Exception {

URL postUrl = new URL(“http://10.0.2.2:1234/Android/ANewsManager.aspx?do=add&name=张三”);

HttpURLConnection connection = (HttpURLConnection) postUrl

.openConnection();

connection.setDoInput(true);

connection.setRequestMethod("GET");

connection.setUseCaches(false);

connection.setInstanceFollowRedirects(true);

connection.setRequestProperty("Content-Type", "text/xml");

connection.connect();

BufferedReader reader = new BufferedReader(new InputStreamReader(

connection.getInputStream(), "utf-8"));// 设置编码,否则中文乱码

String line = "";

StringBuilder sb = new StringBuilder();

while ((line = reader.readLine()) != null) {

sb.append(line);

}

reader.close();

connection.disconnect();

return sb.toString();

}

============解决方案1============

那你的web那边是不是也是使用的utf-8编码呢

============解决方案2============

StringWriter writer = new StringWriter();

IOUtils.copy(conn.getInputStream(), writer,"UTF-8");


你这个方法试试

============解决方案3============

直接用浏览器提交你这个地址 http://10.0.2.2:1234/Android/ANewsManager.aspx?do=add&name=张三
你就能在地址栏看到是编码成什么样子了,也好测试返回的正常与否

============解决方案4============

把UTF-8改成GBK试试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: