0

I'm trying to read from a database and show the result in a TextView in Android, it works until there is a Swedish letter in the sentence, otherwise the TextView shows null. In the database everything is ok, even the Swedish letters work perfect there, but the problem is in the Android.

I'm using UTF-8 in Android and utf8-general-ci for the database column.

Edit: This PHP function solved my problem

/* Change data-type from string to integar or float if required.
 * If string detected then utf8_encode() it. */
function cast_data_types ($value) {
  if (is_array($value)) {
    $value = array_map('cast_data_types',$value);
    return $value;
  }
  if (is_numeric($value)) {
    if(strpos('.', $value)===false) return (float)$value;
    return (int) $value;
  }
  return utf8_encode((string)$value);
}

json_encode (cast_data_types($data));

Salk
  • 1
  • 3

1 Answers1

-1

I think you need to read the value from database as byte[] then encode it to String using new String(bytes,"ISO-8859-1") before showing it.

Ziem
  • 6,579
  • 8
  • 53
  • 86
sanjay
  • 626
  • 1
  • 7
  • 12
  • I'm getting the info by using JSON, like this: JSONObject json_data = new JSONObject(result); pname = (json_data.getString("name")); – Salk Oct 12 '15 at 13:20
  • So what is the value of pname that you are getting? – sanjay Oct 13 '15 at 09:56
  • As I said before, what I get is correct but when the name contains Swedish letters like å ä ö, I get null in the TextView which shows the name. – Salk Oct 13 '15 at 10:31