手机中文字符网络传输的解决方案
2006-02-27 13:53
239 查看
由于手机端使用的是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格式)
思路:将中文字符串转换成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格式)
相关文章推荐
- 手机中文字符网络传输的解决方案//the class of read props
- MySQL中文字符解决方案[Data too long for column XX at row n]
- PHP json_encode() 不支持中文字符的解决方案 ch_json_encode()
- Ubuntu字符界面中文乱码和分辨率的解决方案
- MySQL字符集中文乱码终极解决方案和mysql查询中文问题解决方法[转贴]
- Linux下完美驱动HTC Android手机的Internet传输(通过USB共享电脑网络)
- MySQL字符集中文乱码终极解决方案和mysql查询中文问题解决方法[转贴]
- bootstrap-suggest-plugin (bsSuggest) 联想搜索插件之——中文字符在IE下POST 时乱码现象解决方案
- Thrift 中以GBK传输中文字符和分词服务搭建
- 网络传输 中文乱码问题
- Mysql数据库中文字符乱码解决方案
- UI网络笔记(六):UI网络之去掉非法字符和将URL中的中文字符进行转码
- 网络传输中的中文乱码问题
- android--解决方案--用BroadcastReceiver监听手机网络状态变化
- ajax与中文字符问题完全解决方案
- Cufon中文字符无法识别的解决方案
- ERLEncoder.encode 网络链接中包含中文的解决方案
- URL传递中文字符,特殊危险字符的解决方案(仅供参考)urldecode、base64_encode
- substr截取中文字符出现乱码的解决方案