【发布时间】:2012-04-03 18:26:20
【问题描述】:
我想从 PHP 页面获取一些文本到 Android 应用程序中。只要字符是 ASCII(最多 128 个)就没有问题,但是当我想显示特殊字符时,例如 'Ţ'、'Â'(所谓的 Latin-1 Supplement 和/或 Latin Extended-A - 请参阅Unicode Chart) 我的 Android 应用程序中出现了奇怪的符号 (�)。
问题是我的 Android 应用程序无法处理来自 Latin-1 Supplement 的字符(PHP 可以)但来自 Latin Extended-A 字符集的字符(PHP 不能)。我不知道这两种语言使用什么通用字符集,因为 Java 能够读取 PHP 的字符集不是,反之亦然。
我获取 PHP 页面内容的 Java 函数是这样的:
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;
}
所以,这段代码给了我script.php 页面的内容。如果接收到的文本字符是从 0 到 128 就没有问题。在其他地方,我得到了奇怪的字符。
现在,这是我的 PHP 代码:
<?php
header("Content-Type: text/plain;charset=utf-8");
echo "ÂŞÂĂĒ";
?>
我还有一个 C++ 应用程序,它可以正确读取文本。我一整天都在尝试很多方法,但我就是不明白。谁,在哪里,为什么? 谢谢。
【问题讨论】:
-
也许看看这个stackoverflow.com/questions/4917860/… 和这个stackoverflow.com/questions/3098207/… - 有什么帮助吗?
-
谢谢艾瑞克。现在我可以正确地将它们从 PHP 发送到 Android,但不能从 Android 发送到 PHP。继续努力……
标签: php android special-characters