Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top