android向web提交数据,中文乱码
2014-09-30 16:14
267 查看
====================问题描述====================
源码如下所示,这时候“张三”这个字符到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====================
你这个方法试试
====================解决方案3====================
直接用浏览器提交你这个地址 http://10.0.2.2:1234/Android/ANewsManager.aspx?do=add&name=张三 你就能在地址栏看到是编码成什么样子了,也好测试返回的正常与否
====================解决方案4====================
把UTF-8改成GBK试试
源码如下所示,这时候“张三”这个字符到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试试
相关文章推荐
- android向web提交数据,中文乱码
- android向web提交数据,中文乱码
- Android客户端页面提交数据到tomcat的servlet插入mysql中的信息表遇到的中文乱码问题
- JavaWeb 提交中文数据乱码问题总结
- JAVA_WEB项目之如何解决Struts2和Spring整合时出现中文数据提交时出现乱码
- android客户端提交数据到服务器中文乱码三种解决方案
- Android之WebView加载xml数据及防止中文乱码
- HttpWebRequest模拟POST提交防止中文乱码
- HttpWebRequest模拟POST提交防止中文乱码
- Spring MVC表单提交中文数据出现乱码
- Java web开发中使用get提交表单时的中文乱码问题
- Android中向web服务器提交数据的两种方式四种方法
- Android HttpPost数据中文乱码的解决方案
- android开发中 解决服务器端解析MySql数据时中文显示乱码的情况
- JSP数据提交中文乱码问题
- Android传递数据到web乱码解决 技巧
- 关于处理客户端提交中文数据乱码问题解决对策
- Ajax以Post方式提交数据,中文乱码。
- 页面编码为GBK时_jquery_提交ajax数据完美解决中文乱码方案
- Android通过Get方式提交数据以及乱码处理