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
  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题