ali
ali

Reputation: 11045

Special chars between Android and PHP

I need your help with this one again. I want to send special characters from an Android application to a PHP script and vice versa.

Android Code


...
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("value", "Español"));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = client.execute(post);
    StatusLine statusLine = response.getStatusLine();
    if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
        InputStream inps = response.getEntity().getContent();
        InputStreamReader inp = new InputStreamReader(inps, Charset.forName("ISO-8859-2"));
        BufferedReader rd = new BufferedReader(inp);
        StringBuilder stringBuilder = new StringBuilder();
        String bufferedStrChunk = null;
        while ((bufferedStrChunk = rd.readLine()) != null) {
            stringBuilder.append(bufferedStrChunk);
        }
        Result = stringBuilder.toString();
    }...

PHP Code


<?php
    header("Content-Type: text/plain;charset=ISO-8859-2");
    echo $_POST["value"];
?>

First move

I am trying to send the string "Español" from Android to PHP. I store the string into a MySQL table (UNICODE charset) and I get the string correctly. I also get the integer values of each charracter. The result is "Español" and "69.115.112.97.241.111.108". So, Android sends the character "ñ" as 241, which in the UNICODE Chart is defined as "ń". See it at http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0080

Second move

I return the string from PHP to Android and instead of getting "ñ" I get "ń". Here is where I am lost. When does this change and why having the same numeric value they are different? This is too much for me and I request your help. Thanks in advance!

Upvotes: 4

Views: 2186

Answers (2)

ali
ali

Reputation: 11045

O.K. So this is my final code, and it works. I must specify my environment: API 15, Android 4.0.3

JAVA CODE


...
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("value", "Español"));
    post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "ISO-8859-2"));
    HttpResponse response = client.execute(post);
    StatusLine statusLine = response.getStatusLine();
    if(statusLine.getStatusCode() == HttpURLConnection.HTTP_OK){
        InputStream inps = response.getEntity().getContent();
        InputStreamReader inp = new InputStreamReader(inps, Charset.forName("UTF-8"));
        BufferedReader rd = new BufferedReader(inp);
        StringBuilder stringBuilder = new StringBuilder();
        String bufferedStrChunk = null;
        while ((bufferedStrChunk = rd.readLine()) != null) {
            stringBuilder.append(bufferedStrChunk);
        }
        Result = stringBuilder.toString();
    }
...

PHP CODE


<?php
    header("Content-Type: text/plain;charset=UTF-8");
    echo utf8_encode($_POST["value"]);
?>

With this code I can send that string to the PHP script, store it in a database and/or return it to the Android application (and receive it just as I sent it) I have been working on this for some days and I hope it will be of use for other Android developers. By the way, I use the Java code inside an asynchronous class, inside the doInBackground() function.

Thanks and bye

Upvotes: 0

Che Jami
Che Jami

Reputation: 5151

Use ISO-8859-2 when you create the URLEncodedEntity that you send off. You can set this as a parameter in the constructor.

Without a specified charset, you are probably sending the data in UTF-8/UTF-16 (most common) which the server is interpreting in a different way.

EDIT: It looks like ISO-8859-2 doesn't support ñ. You may have to change something server-side. http://en.wikipedia.org/wiki/ISO/IEC_8859-2

Upvotes: 1

Related Questions