Reputation: 2344
I get a String
from an API
response which I have to show in a TextView
. The String
looks like this:
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:
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
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.
Upvotes: 3
Views: 6047
Reputation: 2370
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.
Upvotes: 2