Network Transmission solution for Chinese characters on mobile phones

Source: Internet
Author: User

Because the mobile phone end uses UTF-8 encoding, so in network transmission requires code conversion, whether from the server to the client, or the client to the server. Next I will write out my solution, which runs successfully on Nokia 7610.
 
Train of Thought: convert a Chinese string to a unicode encoding format (that is, "/uxxxx ......" Format) string for network transmission, after receiving reverse into a Chinese string, so that the phone display or save to the server database, the server receives and sends using ISO8859-1 encoding.
 
Encoding:
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) {// non-Chinese Characters
Hexb = "00" + hexb;
}
Unicodebytes = unicodebytes + "// U" + hexb;
}
// System. Out. println ("unicodebytes is:" + unicodebytes );
Return unicodebytes;
}
 
Decoding:
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); // hexadecimal integer string.
Buffer. append (new character (letter). tostring ());
Start = end;
}
Return buffer. tostring ();
}
 
Example: the client sends registration information to the server and stores it in the database. (The process from the server to the client is the same)
 
Client (using URL encode): receives Chinese characters from the text box, and uses post encoding to connect to the server.
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 ());
.........
 
Enter "White Cloud" in the text box and send it to the server, and output it on wtk:
Get textfield name :??
Encode name:/u767d/u4e91

The server receives and sends data using the ISO8859-1 encoding format.
Httpservletresponse response;
Response. setcontenttype ("text/plain; charset = ISO8859-1 ");
 
Httpservletrequest request;
Request. setcharacterencoding ("ISO8859-1 ");
 
Server reception:
Httpservletrequest request;
Request. setcharacterencoding ("ISO8859-1 ");
Nickname = request. getparameter ("uname ");
System. Out. println ("request get:" + nickname );
Nickname = decodeunicode (nickname );
System. Out. println ("decodename:" + nickname );
 
Server output:
Request get:/u767d/u4e91
Decodename: Baiyun

At this time, the server can use the decoded Chinese string for processing, such as stored in the database (pay attention to the internal code format of the database, such as: into MySQL, the field encoding format can be selected UTF-8 format)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.