URLConnection Character Encoding

I am trying to read a JSON string:

{ "also_known_as": [ " " ], "birthday": "1946-07-06", "deathday": "", } 

over HTTP.

I have the following code:

 URL url = new URL("url"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestProperty("Accept-Charset", "UTF-8");//connection.setRequestProperty("Accept-Charset", "ISO-8859-1"); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = ""; StringWriter writer = new StringWriter(); while((line=reader.readLine())!=null){ writer.write(line); } reader.close(); writer.close(); connection.disconnect(); System.out.println(writer.toString()); 

But it prints a line in the console:

 { "also_known_as": [ "БильвСÑ?Γ‘β€šΓ‘β‚¬ ΓΒ‘Γ‘β€šΓΒ°ΓΒ»ΓΒ»ΓΒΎΓΒ½ΓΒ΅" ], "birthday": "1946-07-06", "deathday": "", } 

I also tried:

 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));//BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "ISO-8859-1")); 

But no luck.

My question is how to set character encoding in URLConnection?

Any information would be very helpful to me.

Sincerely.


Using Apache IOUtils I tried this:

 StringWriter writer = new StringWriter(); IOUtils.copy(connection.getInputStream(), writer, "UTF-8"); 

But it prints the same result in the eclipse console.


Using Apache HttpClient:

 DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet getRequest = new HttpGet("http://api.themoviedb.org/3/person/16483?api_key=23e89da030a0ee8b25aaed20950a0c25"); getRequest.addHeader("accept", "application/json"); HttpResponse response = httpClient.execute(getRequest); StringWriter writer = new StringWriter(); IOUtils.copy(response.getEntity().getContent(), writer, "UTF-8"); System.out.println(writer.toString()); 

same result.

+4
source share
2 answers

Just pull my comment into the answer, which turned out to be the reason: the console character set was Cp1252, so the output was correct, but it did not display correctly.

+5
source

Do it:

 new InputStreamReader(connection.getInputStream(), new Charset("UTF-8")) 

i.e. specify this encoding.

+1
source

Source: https://habr.com/ru/post/1399572/


All Articles