手机中文字符网络传输的解决方案

来源:互联网 发布:miss淘宝零食店 编辑:程序博客网 时间:2024/04/28 02:32

由于手机端使用的是UTF-8编码,所以在网络传输中需要进行码制转换,无论是从服务器到客户端,还是客户端到服务器端。下面我写出我的解决方法,该方法在Nokia 7610上运行成功。
 
思路:将中文字符串转换成Unicode编码格式(即”/uXXXX/uXXXX……”形式)的字符串进行网络传输,在接收后反转换成中文字符串,以便在手机上显示或者存到服务器端数据库中,服务器接收和发送使用ISO8859-1编码。
 
编码:
       public static String encodeUnicode(final String gbString)
       {
              char[] utfBytes = gbString.toCharArray();
              String unicodeBytes = "";
              for (int byteIndex = 0; byteIndex < utfBytes.length; byteIndex++) {
                     String hexB = Integer.toHexString(utfBytes[byteIndex]);
                     if (hexB.length() <= 2) {       //非中文字符
                            hexB = "00" + hexB;
                     }
                     unicodeBytes = unicodeBytes + "//u"+ hexB;
              }
//            System.out.println("unicodeBytes is: " + unicodeBytes);
              return unicodeBytes;
       }
 
解码:
       public static String decodeUnicode(final String dataStr)
       {
              int start = 0;
              int end = 0;
              StringBuffer buffer = new StringBuffer();
              while (start > -1) {
                     end = dataStr.indexOf("//u", start + 2);
                     String charStr = "";
                     if (end == -1) {
                            charStr = dataStr.substring(start + 2, dataStr.length());
                     } else {
                            charStr = dataStr.substring(start + 2, end);
                     }
                     char letter = (char) Integer.parseInt(charStr, 16); // 16进制整形字符串。
                     buffer.append(new Character(letter).toString());
                     start = end;
              }
              return buffer.toString();
       }
 
示例:客户端发送注册信息到服务器,存储到数据库中。(服务器到客户端处理是一样的)
 
客户端(使用url encode):从文本框中接收中文字符,编码使用POST方式连接服务器。
TextField yourName = null;
StringBuffer oBuf = new StringBuffer();
   ………
   //name
String temp = yourName.getString().trim();
System.out.println("Get textfield name :" + temp );
temp = encodeUnicode (temp);
System.out.println("Encode name:" + temp);
oBuf.append("UName=" + temp);
  ………
HttpConnection con = null;
OutputStream os = null;
   ………
con.setRequestMethod(HttpConnection.POST);
on.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
os = con.openOutputStream();
os.write(oBuf.toString().getBytes());
………
 
在文本框中输入“白云”发送到服务器,在WTK上输出:
Get textfield name :??
Encode name:/u767d/u4e91

服务器端使用”ISO8859-1”编码格式接收和发送数据。
HttpServletResponse response;
response.setContentType("text/plain; charset=ISO8859-1");
 
HttpServletRequest request;      
request.setCharacterEncoding("ISO8859-1");
 
服务器接收:
HttpServletRequest request;      
request.setCharacterEncoding("ISO8859-1");
nickName = request.getParameter("UName");
System.out.println("Request get:" + nickName);
nickName = decodeUnicode(nickName);
System.out.println("DecodeName :" + nickName);     
 
服务器输出:
Request get:/u767d/u4e91
DecodeName :白云

这时在服务器就可以使用解码后的中文字符串进行处理了,比如存入数据库(要注意数据库的内码格式,如:存入MySQL,字段编码格式可选择UTF-8格式)

原创粉丝点击