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