5

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.

2

1 Answer 1

1

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

mysql_query("SET NAMES 'utf8'");
Sign up to request clarification or add additional context in comments.

2 Comments

O.K. Javo. I will keep that in mind for when I will get text from the database. For now I just want to get it from a text file. Thanks
Be sure of the encoding in your text file, too. Some text editors let you see / set the encoding. Also, I suggest this perhaps naively, have you considered encoding everything in UTF-8? Android uses UTF-8 (stackoverflow.com/questions/4564688/…) and there's plenty of resources for handling UTF-8 with PHP...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.