Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Post converts € to ? symbol

Tags:

android

http

php

My Android app communicates via HTTP Post with a PHP server. I add to the HTTP request following parameter:

   nameValuePairs.add(new BasicNameValuePair("text", message));

message is a String and contains the symbol €

On the server PHP is running and gets the request. Unfortunatelly the € symbol is automatically converted to ? symbol. All other symbols are working like "ä, ü, $, ß

On Android I have set no encoding:

  HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://server.com/test.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

On the PHP site I have also nothing specified. Here the code:

<?php

mysql_connect("blablaost.com", "blabla", "blabla") or die(mysql_error());
mysql_select_db("asfd") or die(mysql_error());
$mysqldate = gmdate( 'Y-m-d H:i:s');

$language = (int) $_REQUEST['language'];

mysql_query("blabla ... .$_REQUEST['text']. ") 
or die(mysql_error());  

mysql_close();

?>

$_REQUEST['text'] contains the € and it gives me a ?

like image 542
tobias Avatar asked Jan 04 '12 01:01

tobias


1 Answers

The second construction parameter of UrlEncodedFormEntity is the transmit encoding. Replace with:

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

Also, make sure that the Web page that displays the value reports a charset to the browser and conforms to that charset. To report the charset to the browser, use either

header("Content-Type: text/html;charset=UTF-8", true);

in PHP, or

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

element under HTML's <head>. The former trumps the latter, if both are provided.

The actual encoding that the PHP file has (i. e. the encoding of non-ASCII characters in it) has to match the one PHP file claims as its Content-Type. Depending on your PHP editor, there may be different ways to set the encoding of the file. In Visual Studio, for example, there's a "Save with encoding" command.

EDIT re: unrelated issue:

To remove PHP magic quotes, I use the following function:

function deq($s) //Stands for "dequote"
{
    if($s == null)
        return null;
    return
        get_magic_quotes_gpc() ?
        stripslashes($s) : $s;
}

And then instead of $_POST["xxx"], I use deq($_POST["xxx"]) where appropriate. Since the server settings may change (and the server itself might change if you migrate), the dequote function must take the current value of the setting into account.

like image 123
Seva Alekseyev Avatar answered Oct 18 '22 15:10

Seva Alekseyev