2012-04-05 74 views
4

我需要你再次帮助这个。我想从Android应用程序发送特殊字符到PHP脚本,反之亦然。Android和PHP之间的特殊字符

的Android代码


... 
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代码


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

第一招

我想从Android发送字符串“Español”到PHP。我将该字符串存储到一个MySQL表(UNICODE字符集)中,并正确地获取了字符串。我也得到每个字符的整数值。结果是“Español”和“69.115.112.97.241.111.108”。所以,Android发送字符“ñ”为241,在UNICODE Chart中定义为“ñ”。看到它的http://www.ssec.wisc.edu/~tomw/java/unicode.html#x0080

第二招

我从PHP返回字符串到Android和,而不是让“N”我得到的“N”。这是我迷失的地方。这种变化是什么时候,为什么有相同的数值,他们是不同的?这对我来说太过分了,我请求你的帮助。提前致谢!

+0

为什么你不使用UTF-8? – Rufinus 2012-04-05 13:11:57

+1

在发送任何东西之前使用'base64'。我做了什么以及它的工作 – Baba 2012-04-05 13:13:22

回答

1

使用ISO-8859-2当您创建您发送的URLEncodedEntity。您可以将其设置为构造函数中的参数。

如果没有指定的字符集,可能是以UTF-8/UTF-16(最常见的)方式发送服务器以不同方式解释的数据。

编辑:看起来像ISO-8859-2不支持ñ。你可能不得不改变服务器端的东西。 http://en.wikipedia.org/wiki/ISO/IEC_8859-2

+0

O.K.人。问题解决了!谢谢!只要我被允许,我会尽快为大家发布解决方案。再次感谢! – ali 2012-04-05 18:14:30

0

O.K. 所以这是我的最终代码,它的工作原理。我必须指定我的环境:API 15,安卓4.0.3

Java代码


... 
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代码


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

使用本C ode我可以将该字符串发送给PHP脚本,将其存储在数据库中和/或将其返回给Android应用程序(并按照发送它的方式接收它) 我一直在研究这个问题,并希望它能够适用于其他Android开发人员。顺便说一下,我在doInBackground()函数内部的异步类中使用了Java代码。

感谢和再见