×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    Question Transferring unicode data through HTTP

    Hey there,
    I'm pretty stuck on this one so any help would be much appreciated.
    I'm tryin to make an application which sends/receives unicode text(for example "\u06XX \u06XX" which is actually some Arabic unicode) to/from a HTTP server.
    I can normally display the string from within the application but when it comes to response text, I get the string itself (\u06XX \06XX) printed out. I usually use sth like the following method to handle my http post actions:
    Code:
    private String sendHttpPost( String url, String whattopost)
            {
            HttpConnection      hcon = null;
            DataInputStream     dis = null;
            DataOutputStream    dos = null;
            StringBuffer        responseMessage = new StringBuffer();
    
            String              requeststring = whattopost;
    
            try {
                
                hcon = ( HttpConnection )Connector.open( url, Connector.READ_WRITE );
    
                
                hcon.setRequestMethod( HttpConnection.POST );
    
                
                dos = hcon.openDataOutputStream();
                byte[] request_body = requeststring.getBytes();
    
                
                for( int i = 0; i < request_body.length; i++ ) {
                    dos.writeByte( request_body[i] );
                }
    
                
                dis = new DataInputStream( hcon.openInputStream() );
            
    
                
                int ch;
                while( ( ch = dis.read() ) != -1 ) {
                    responseMessage.append( (char)ch );
                }
            }
            catch( Exception e )
            {
                e.printStackTrace();
                responseMessage.append( "ERROR" );
            } 
            finally {
                
                try {
                    if( hcon != null ) hcon.close();
                    if( dis != null ) dis.close();
                    if( dos != null ) dos.close();
                } catch ( IOException ioe ) {
                    ioe.printStackTrace();
                }
            }
            return responseMessage.toString();
            }
    I need to somehow convert the byte-by-byte incoming data to normal unicode string.
    I've already tried defining a "UTF-8" encoding type, that didn't help.
    Thanks for your time
    Parsa

  2. #2
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: Transferring unicode data through HTTP

    Hi Parsa,
    When you are reading the unicode data . you have to use it's encoding .
    1. In your code replace
    byte[] request_body = requeststring.getBytes();
    With
    byte[] request_body = requeststring.getBytes("UTF-8");

    this is to send the unicode data.

    For receiving the unicode thing read your data in to byte array and use the encoding to get is appropriate encoding to convert the encoding value.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    Re: Transferring unicode data through HTTP

    Thanks for your reply Bhanu.
    I did the replacement. And as for receiving data, I wrote sth like:
    Code:
    private String decodeUnicode(String unicodeSrc)    {
        StringBuffer sb = new StringBuffer();
        for( int i = 0; i < unicodeSrc.length(); i=i+4 ) {
             System.out.println(i);
             sb.append(convertStrToUnicode(unicodeSrc.substring(i,i+4)));
        }
        return sb.toString();
    }
    and convertStrToUnicode is:
    Code:
    private String convertStrToUnicode(String value) {
        short valueAsShort = Short.parseShort(value.trim(),16);
        return String.valueOf((char)valueAsShort);
      }
    I also strip the "\u" tags in the server so it's easier for the MIDlet to process data.
    It seems solved and is working fine, but is there any suggestions about two above methods ?
    Thanks,
    Parsa

  4. #4
    Regular Contributor
    Join Date
    Jul 2007
    Posts
    53

    Re: Transferring unicode data through HTTP

    Parsa,

    It is unfortunate that the website is not encoded using UTF-8 which really is the preferred way for internationalized content to be served up on the web. Instead, it seems, it is being served up as escaped unicode which means, apart from everything else that the Arabic strings are going to result in much heavier network traffic than a more efficient UTF-8.

    Some Questions :

    Where are you stripping the \u prefixes on the server ? Have you a PHP script or servlet which is doing this before the content is sent back to the client. It would be far better for that script to re-encode the document in UTF-8 before sending back to the mobile. The mobile could then using the UTF-8 decoder code mentioned earlier in the thread.

    A drawback of your current approach (apart from higher data transfer costs to the end-user than is necessary) is that you won't be able to mix English/latinized characters with the Arabic content unless you escape them also. Your mobile code which attempts to decode the "unicode" strings assumes that every character in the content downloaded will be 4 characters long, i.e a unicode \uXXXX with the first 2 characters stripped away.

    What if the website contained something like AB\u0661\u0662
    Your decoder wouldn't necessarily know to treat the first two
    characters differently than the second because when you strip away the leading \u your decoder would be handed
    "AB06610662"

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    12

    Re: Transferring unicode data through HTTP

    Ipxlittle,
    I understand the issues you mentioned.
    As for the first question, Yes I'm sending the response through a php script with UTF-8 encoding which handles the stripping thing as well.
    About your second question, it's all a matter of data input cuz it's a closed system. The data taken from user's handset (as \uXXXX) is transmitted to the php script and it stores it in a db and when the user asks for the proper data, it is send via php script and all. So I intend to use an on-screen keyboard to prevent user from sending unwanted characters such as "AB".
    I hope it works. Any idea ?
    Thanks,
    Parsa

Similar Threads

  1. Accessing the data in a HTTP filter
    By aquarian3183 in forum Symbian Networking & Messaging (Closed)
    Replies: 4
    Last Post: 2008-09-18, 06:24
  2. Replies: 3
    Last Post: 2007-03-24, 16:53
  3. Http send and receive data
    By white_dragon in forum Symbian
    Replies: 1
    Last Post: 2005-02-02, 10:01
  4. Http send and receive data
    By white_dragon in forum Symbian Networking & Messaging (Closed)
    Replies: 2
    Last Post: 2004-11-17, 06:53
  5. Using Bluetooth to send data to a nokia 6310 and then sending it over http
    By studentproject in forum Bluetooth Technology
    Replies: 1
    Last Post: 2002-11-01, 08:51

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×