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.