From a5330746e6a633e337c0e59ce16bcad8a29d67c1 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 30 Aug 2016 18:39:57 +0200 Subject: [PATCH] CustomHTTPClient, no easy way of retrieving the charset from urlConnection.getContentType() --- .../gumartinm/weather/information/httpclient/CustomHTTPClient.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java b/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java index 275706a..61656e5 100644 --- a/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java +++ b/app/src/main/java/name/gumartinm/weather/information/httpclient/CustomHTTPClient.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.Charset; public class CustomHTTPClient { private final String userAgent; @@ -35,7 +36,10 @@ public class CustomHTTPClient { try { final InputStream in = new BufferedInputStream(urlConnection.getInputStream()); final ByteArrayOutputStream buffer = readInputStream(in); - return new String(buffer.toByteArray() /**, contentType.getCharset()**/); + // No easy way of retrieving the charset from urlConnection.getContentType() + // Currently OpenWeatherMap returns: application/json; charset=utf-8 + // Let's hope they will not change the content-type :/ + return new String(buffer.toByteArray(), Charset.forName("UTF-8")); } finally { urlConnection.disconnect(); } -- 2.1.4