3

I get a String from an API response which I have to show in a TextView. The String looks like this:

enter image description here

It should be "Bürling" but the German character ü is not being represented.

The problem is that I don't know how this String is encoded on the server.

I've tried converting it to UTF-8, windows-1252 and ISO-8859-1 in all the possible combinations with no success.

I've also read this great article:

What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text

Here some of the links I've tried:

How do I convert between ISO-8859-1 and UTF-8 in Java?

Java convert Windows-1252 to UTF-8, some letters are wrong

Encode String to UTF-8

Does anyone has an idea?
Thanks.

EDIT:

I found the reason. Parsing the response from the API, I just added this:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), Charset.forName("ISO-8859-15")));

Before it was like this:

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

Now the ü character is displayed properly.

Thanks a lot for all the comments, it made me research in the right direction.

Community
  • 1
  • 1
Ale
  • 2,282
  • 5
  • 38
  • 67
  • 3
    We don't know how you're *fetching* the data from the server - that's one of the important parts. Please give more context. – Jon Skeet Feb 17 '17 at 10:31
  • 1
    `I get a String from an API response` That's where the problem lies. The API is most likely using the default Windows encoding: CP1252. Instead, you need the value to be properly returned as UTF-8. – Phantômaxx Feb 17 '17 at 10:33
  • 1
    if no info in HTTP headers, did you try [chardet](http://chardet.readthedocs.io/en/latest/usage.html)? – pskink Feb 17 '17 at 10:40
  • 1
    Thanks guys your comments helped me to look at the right direction and find the problem. – Ale Feb 17 '17 at 11:16
  • http://stackoverflow.com/questions/32696273/java-replace-german-umlauts/32709513#32709513 – PEHLAJ Feb 17 '17 at 11:34
  • If you are using MySQL, see "black diamonds" in http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Feb 20 '17 at 01:25

1 Answers1

2

I think blindly trying to convert to a random charset won't do you any good before you actually know which charset is being used by the server.

You should deduce the charset sent by the API from the Content-type response headers. Or even ask for one in the request.

Jeremy Grand
  • 2,300
  • 12
  • 18