doukong5394
2014-05-08 03:40
浏览 47

从php发送到android的特殊字符打印错误

I have a MySQL database, in wich i have a table with a column whith a collation of:

utf8_general_ci

In this column i am saving special characters like:

á, é, í, ó, ú, ñ

When i get this column using PHP and print them in a web page, everything looks perfect, the problem comes when i print those chracters on my android app, it looks like this:

ñ

And wierd characters like that.

I have tried to solve this using:

mysql_query("set names 'utf8'");

But i havent luck. How can i solve this? At first i think it was a MySQL problem, but i can see that that the data is printed correct when its on a web page, so maybe its android...

I have the next code in android:

HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(Utils.IP_ADDRESS + "index.php?module=fetchImage&page=main&ajax=1");
        List<NameValuePair>  nameValuePair = new ArrayList<NameValuePair>(1);
        nameValuePair.add(new BasicNameValuePair("sessionId", prefs.getString("sessionId", null)));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        }
        catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HttpResponse response = httpClient.execute(httpPost);
        responseBody = EntityUtils.toString(response.getEntity());
        System.out.println(responseBody); // here i get Otoño and it should be Otoño

图片转代码服务由CSDN问答提供 功能建议

我有一个MySQL数据库,我有一个表格,其中的列有一个整理的列:

  utf8_general_ci 
   
 
 

在此栏中,我将保存特殊字符,例如:

 < 代码>á,é,í,ó,ú,ñ
   
 
 

当我使用PHP获取此列并将其打印在网页中时,一切看起来都很完美, 当我在我的Android应用程序上打印这些字符时出现问题,它看起来像这样:

 ñn   
 
 

和那些奇怪的人物一样。

我试图用以下方法解决这个问题:

  mysql_query(“set names'utf8'”);  
   
 
 

但我没有运气。 我怎么解决这个问题? 起初我认为这是一个MySQL问题,但我可以看到数据在网页上打印时是正确的,所以也许它的android ...

我有下一个 android中的代码:

  HttpClient httpClient = new DefaultHttpClient(); 
 HttpPost httpPost = new HttpPost(Utils.IP_ADDRESS +“index.php?module = fetchImage&amp; page = main&amp; amp;  ; ajax = 1“); 
 List&lt; NameValuePair&gt;  nameValuePair = new ArrayList&lt; NameValuePair&gt;(1); 
 nameValuePair.add(new BasicNameValuePair(“sessionId”,prefs.getString(“sessionId”,null))); 
尝试{
 httpPost.setEntity(new UrlEncodedFormEntity(  nameValuePair)); 
} 
 catch(UnsupportedEncodingException e){
 e.printStackTrace(); 
} 
 HttpResponse response = httpClient.execute(httpPost); 
 responseBody = EntityUtils.toString(response.getEntity(  )); 
 System.out.println(responseBody);  //这里我得到Otoño它应该是Otoño
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drtoclr046994545 2014-05-08 07:10
    已采纳

    The problem is not in Your MySQL or PHP, because its retrieve and print the Special characters correctly in Webpage.

    It seems that the problem is in your android client code. You are not parsing the response to correct charset (UTF-8).

    If the response from web service is JSON, its default charset is UTF-8, so you need to read the response from ws as UTF8, for Example

    BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "utf-8"), 8);
    

    If you give additional details or sample codes, Its even more easier to solve your problem

    EDIT

    Based on your coding, change

    responseBody = EntityUtils.toString(response.getEntity());

    to

    responseBody = EntityUtils.toString(response.getEntity(), "utf8");

    You need to mention the Charset while converting your response to String.

    点赞 评论

相关推荐 更多相似问题