Android将String类型字符串转换成utf-8,gbk格式

来源:互联网 发布:ubuntu查看防火墙状态 编辑:程序博客网 时间:2024/06/05 09:15

最近在做Android的后台时出现手机提交到中文出现乱码,通过以下两步解决了问题。 
1:修改 Tomact目录下的Service.xml (安装目录下的\conf下)文件: 
添加:URIEncoding="UTF-8"

<Connector port="8080" protocol="HTTP/1.1"               connectionTimeout="20000"               redirectPort="8443" URIEncoding="UTF-8" />
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" URIEncoding="UTF-8" />
  • 1
  • 1

2:将要传输的内容转换成上文修改的格式(当然要确保数据库的格式也是一样的)

try {              String strGBK = URLEncoder.encode(str, "GBK");              System.out.println(strGBK);              String strUTF8 = URLDecoder.decode(str, "UTF-8");              System.out.println(strUTF8);          } catch (UnsupportedEncodingException e) {              e.printStackTrace();          } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

OK了。

阅读全文
0 0
原创粉丝点击