Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User a_nemoff's Avatar
    Join Date
    Apr 2006
    Posts
    9
    Hi,

    I'm doing a MIDlet that retrieves data from a PHP script but I have problems with european and special characters like é, è, ç, à, ô, ï, °, €, ...

    Here is my PHP script (very simple, just for testing) :

    PHP Code:
    <?php
    $content 
    "éèçàôï";
    header"Content-Type: text" );
    header"Content-Length: ".(string)( strlen$content ) ) );
    echo 
    $content ;
    ?>
    I have the same problem if I read data from a .txt file wich is on the server.

    For example if have this string :
    "Test: euro € - n°1 - ça va bien ? oui, très!"

    I obtain:
    "Test: euro ? - n?1 - ?a va bien ? oui, tr?s!"

    In the console I have '?', but in the emulator screen I have a square symbol instead of the '?'. Anyway I don't get the right character ...

    I guess that the problem is in my java code but I don't know how to solve it. I think the problem comes from casting the data bytes into char.

    Here is my code :
    Code:
    HttpConnection httpC = null;
    DataInputStream din = null;
    int contentLength= 0;
    byte data[] = null;
    StringBuffer sb = new StringBuffer();
    
    try {
    
    // Opening connexion httpC = (HttpConnection)Connector.open( url, Connector.READ_WRITE ); // Opening DataInputStream to get the data din = httpC.openDataInputStream(); // Getting the content length contentLength= (int)httpC.getLength(); // Reading the data: // Data length is known : data is stored directly in the byte array if ( contentLength> 0 ) {
    data = new byte[contentLength]; din.readFully( data );
    } // Content length is unknown : we read the data until we reach EOF else {
    int ch; while ( ( ch = din.read() ) != -1 ) {
    sb.append((char)ch);
    }
    }
    } catch ( ClassCastException e ) {
    throw new IllegalArgumentException("Not an HTTP URL");
    } catch ( IOException ex ) {
    ex.printStackTrace();
    } finally {
    // Closing streams try {
    if( din != null ) din.close(); if( httpC != null ) httpC.close();
    } catch ( IOException ex ) {
    ex.printStackTrace();
    }
    } if( tailleData > 0) {
    // If data was read using .readFully() I transfer it to the StringBuffer // using some cast operations. char charData[] = new char[data.length]; for (int i = 0; i < data.length; i++) {
    charData[i] = (char)data[i]; System.out.print(charData[i]);
    } sb = new StringBuffer( data.length ); sb.append( charData );
    }
    Actually char are defined as int from -127 to 127 and that's why I get the '?' mark instead of the real characters, because those characters are coded as int from 0 to 256.

    Please let me know if you know how to solve this problem.
    Thanks.
    Regards

  2. #2
    Regular Contributor axs's Avatar
    Join Date
    Aug 2006
    Posts
    307
    Try to use java.io.InputStreamReader instead of DataInputStream. The reason is that Readers and Writers are working with Unicode, while DataInput- and DataOutputStreams are better for ASCII characters only.

    Also don't forget to specify used encoding while putting data out of your PHP script, and remember to construct your InputStreamReader with the same encoding-name.

  3. #3
    Registered User a_nemoff's Avatar
    Join Date
    Apr 2006
    Posts
    9
    Ok thanks i'll try that ;-)

Similar Threads

  1. FileConnection/InputStream reading problem
    By badquek in forum Mobile Java General
    Replies: 1
    Last Post: 2006-06-28, 17:51
  2. Problem in receiving SMS of 160 characters
    By naoriya in forum General Messaging
    Replies: 3
    Last Post: 2003-12-24, 01:15
  3. Replies: 1
    Last Post: 2002-08-28, 04:35
  4. Problem with Greek Characters on Input
    By Nokia_Archived in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2002-05-15, 00:38

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved