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.

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

报告相同问题?

悬赏问题

  • ¥60 Python如何后台操作Vmwake虚拟机键鼠
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容