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!
Use ISO-8859-2 when you create the
URLEncodedEntitythat 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