Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special characters between Android application and PHP script

I want to get some text from a PHP page into an Android application. As long as the characters are ASCII (up to 128) there's no problem but when I want to display special characters like 'Ţ', 'Â' (the so called Latin-1 Supplement and/or Latin Extended-A - see it at Unicode Chart) I get weird symbols into my Android application (�).

The problem is that my Android application cannot handle characters from Latin-1 Supplement (which PHP can) but characters from the Latin Extended-A charset (which PHP cannot). I don't know what common charset for both languages to use because the one that Java is able to read PHP is not, and vice versa.

My Java function that gets the content of the PHP page is this:

public static String GetData() {
    byte[] Bresult = null;
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://www.mypage.com/script.php");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("var", "val"));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        HttpResponse response = client.execute(post);
        StatusLine statusLine = response.getStatusLine();
        if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
            Bresult = EntityUtils.toByteArray(response.getEntity());
            Result = new String(Bresult, "UTF-8");
        }
    } catch (UnsupportedEncodingException e) {
        Log.w("MYAPP", "Unsupported Encoding Exception (" + e.getMessage() + ")");
    } catch (Exception e) {
        Log.w("MYAPP", "Exception (" + e.getMessage() + ") ocurred");
    }
    return Result;
}

So, this code gives me the content of the script.php page. If the characters of the text received are from 0 to 128 there is no problem. Elsewhere, I get weird characters.

Now, this is my PHP code:

<?php
    header("Content-Type: text/plain;charset=utf-8");
    echo "ÂŞÂĂĒ";
?>

I also have a C++ application and it does read the text correctly. I have been trying a lot of methods all the day and I just don't get it. Who, where, why? Thanks.

like image 978
ali Avatar asked Nov 14 '22 07:11

ali


1 Answers

where are you getting the data?, if is through a mysql database you must specify UTF8 like this

mysql_query("SET NAMES 'utf8'");
like image 148
Javier Avatar answered Nov 16 '22 03:11

Javier