×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Lithuania
    Posts
    14

    Question Q: read UTF-8 data from servlet

    Hi,

    I have servlet:
    Code:
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
             response.setContentType("text/plain;charset=UTF-8");
                String message ="LT characters: čėįšųū, some Chine: \u65e5\u672c\u8a9e\u6587\u5b57\u5217";
                response.setContentLength(message.length());
                PrintWriter out = response.getWriter();
                out.println(message);
                out.close();
                out.flush();
        }
    I want to read the message in j2me with correct characters(tested only in NB emulator with System.out.println()) . I tried all this examples, but no success
    http://j2mesamples.blogspot.com/2009...sing-j2me.html
    http://www.java-samples.com/showtuto...tutorialid=735
    http://stackoverflow.com/questions/1...ttp-using-midp
    http://72.5.124.102/thread.jspa?thre...25&tstart=1650
    http://72.5.124.102/thread.jspa?messageID=9946528
    http://www.wirelessdevnet.com/channe...2me_http.phtml

    can anybody give me a working example?

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Q: read UTF-8 data from servlet

    Quote Originally Posted by brasta78 View Post
    Code:
                String message ="LT characters: čėįšųū, some Chine: \u65e5\u672c\u8a9e\u6587\u5b57\u5217";
    Beware of putting any non-ASCII characters in source code. Use unicode escapes ("\uxxxx") for any non-ASCII character. Otherwise, your code might compile differently on different platforms (depending on the "platform default encoding").

    Check this article for how to read a byte[] from an HttpConnection's InputStream.

    Are you sure the PrintWriter you created is using UTF-8?

    Once you get the byte[], convert it to a String like:

    Code:
    String s = new String(byteArray, "UTF-8");
    Graham.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Location
    Lithuania
    Posts
    14

    Re: Q: read UTF-8 data from servlet

    thanks, grahamhughes, for response
    In my case String message will be result from db.
    I tried this example, but still getting result with: ????
    Code:
    cn = (HttpConnection) Connector.open(url);
                                stream = cn.openInputStream();
                                System.out.println("cn enc:" + cn.getEncoding());
                                byte[] data = new byte[0];
                                byte[] buffer = new byte[1024];
                                int bytesRead;
                                while ((bytesRead = stream.read(buffer)) > 0) {
                                    byte[] newData = new byte[data.length + bytesRead];
                                    System.arraycopy(data, 0, newData, 0, data.length);
                                    System.arraycopy(buffer, 0, newData, data.length, bytesRead);
                                    data = newData;
                                }
                                String s = new String(data, "UTF-8");
                                System.out.println("rez: " + s);
    In servlet response.getCharacterEncoding() returns UTF-8, but in my midelt cn.getEncoding() returns null. maybe there is some trouble?

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Q: read UTF-8 data from servlet

    getEncoding() gets the content-encoding for the response, not the character encoding, so they won't be the same.

    Remember that when you use System.out.println(), you might be printing to a stream that doesn't support Unicode, so you might not see the correct characters. There's also a fairly good chance that your phone (and possibly your computer) won't display Chinese characters. Try coding the same "message" string into your MIDlet, and compare them.

    Try using String.toCharArray(), and check the character codes. You might have everything working already.

    Graham.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Location
    Lithuania
    Posts
    14

    Thumbs up Re: Q: read UTF-8 data from servlet

    sorry - all is OK. I wrote, that I tested only with System.out.println in midlet. When I set result to midlet item: StringItem.setText(s) all chatacters are displaying correct.

    example:
    from servlet I pass this result with my language special characters: message = "ąčėįšųū"
    in midlet with command System.out.println I get result: ?????
    in midlet setting displayable component StringItem.setText(s) display correct characters: "ąčėįšųū"

Similar Threads

  1. Nokia N95 - annoying 'read user data' prompts
    By benuk in forum Mobile Java Networking & Messaging & Security
    Replies: 14
    Last Post: 2010-11-05, 10:57
  2. Cannot send data to the servlet in emulator!
    By sweetsweet24 in forum Mobile Java General
    Replies: 2
    Last Post: 2008-10-08, 03:48
  3. Read() not able to get data and RecvOneOrMore() some time gets some time not!!
    By jyotideep in forum Symbian Networking & Messaging (Closed)
    Replies: 5
    Last Post: 2007-11-06, 09:52
  4. The error of read data from file
    By balloon22607 in forum Symbian
    Replies: 1
    Last Post: 2007-04-02, 12:25
  5. Replies: 1
    Last Post: 2006-01-02, 16:58

Posting Permissions

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