Reputation: 1435
I have a couple of string that aren't converting properly. The issue that I had was that all my strings had weird characters on it (They are in spanish with the top accent) and I was able to convert them with the following code:
Connection to DB: After is connected I proceed to get the information from the DB as follows (it comes with json format) DB information shows as Perú:
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
//BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
//is.close();
Log.i("Tag:", result);
}
retrieve JSON list:
try{
jArray = new JSONArray(welcome.result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "title:" + json_data.getString("title"));
try {
country = new String(json_data.getString("country").getBytes("ISO-8859-1"), "UTF-8");
}
catch (UnsupportedEncodingException e){
}
However I grabbed another string in spanish and it reverted back but the other strings still show up properly. The current string looks like this PER�?º Any ideas? Im guessing is using a different encoding. The database where I'm pulling them from uses utf8_general_ci. Thank you in advance!
Upvotes: 0
Views: 183
Reputation: 159250
Trying to make sense of what you show.
You say database shows Perú
.
In CP1252, that is the bytes 50 65 72 C3 83 C2 BA
.
Those bytes as UTF-8 is Perú
.
In CP1252, that is the bytes 50 65 72 C3 BA
.
Those bytes as UTF-8 is Perú
. <== Right value
It would seem you have a double bad encoding going on here.
One bad encoding could be from "database shows", i.e. how do you see what database shows?
Other bad encoding is that whoever inserted the data messed up.
I would postulate that database actually has Perú
stored in UTF-8. The tools you use to show the value is doing it wrong. Of course, whoever inserted the value was doing it wrong too, hence the double error.
Solution: You start by fixing the code that inserted bad data. You also use a tool that can correctly show what's in the database.
Then you remove any encoding hacks from your Java code, because it very likely is doing it right to being with. Or at least, it's not the Java to database part of the code that is bad.
Upvotes: 2
Reputation: 45005
Simply do this:
country = json_data.getString("country");
getString
returns already a String
so no need to encode it into ISO-8859-1
to decode it into UTF-8
which cannot work for obvious reasons.
If you get incorrect characters even with the code above, it means that your problem comes before, probably while parsing your JSON
content, it has probably not been parsed with the correct character encoding.
You need to explicitly set the character encoding to UTF-8
otherwise it will use the default one as next:
BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
Upvotes: 2