From 63ed620017ceb53bd2b66c4d3e01bf2f24fc953f Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Tue, 30 Aug 2016 20:10:48 +0200 Subject: [PATCH] CustomHTTPClient, little improvements. --- .../weather/information/httpclient/CustomHTTPClient.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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 61656e5..8ef0f6a 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 @@ -31,17 +31,18 @@ public class CustomHTTPClient { } public String retrieveDataAsString(final URL url) throws IOException { - final HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); - urlConnection.setRequestProperty("User-Agent", userAgent); + final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try { - final InputStream in = new BufferedInputStream(urlConnection.getInputStream()); + connection.setRequestProperty("User-Agent", userAgent); + connection.setRequestProperty("Cache-Control", "no-cache"); + final InputStream in = new BufferedInputStream(connection.getInputStream()); final ByteArrayOutputStream buffer = readInputStream(in); // 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(); + connection.disconnect(); } } @@ -58,7 +59,7 @@ public class CustomHTTPClient { return byteBuffer; } - public static final CustomHTTPClient newInstance(String userAgent) { + public static final CustomHTTPClient newInstance(final String userAgent) { return new CustomHTTPClient(userAgent); } } -- 2.1.4