From a9ce72ba41b137398534cf91597228ea5b21256e Mon Sep 17 00:00:00 2001 From: Prominence Date: Sat, 7 Jul 2018 21:29:58 +0300 Subject: [PATCH] Got rid of 'httpclient' dependency. --- pom.xml | 9 ----- .../provider/AbstractOpenWeatherProvider.java | 34 +++++++------------ .../openweather/api/utils/JsonUtils.java | 2 ++ 3 files changed, 15 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 542be16..a62a3ca 100644 --- a/pom.xml +++ b/pom.xml @@ -8,15 +8,7 @@ by.prominence.openweater.api 1.0-SNAPSHOT - - - - org.apache.httpcomponents - httpclient - 4.5.5 - - com.alibaba fastjson @@ -24,5 +16,4 @@ - \ No newline at end of file diff --git a/src/main/java/by/prominence/openweather/api/provider/AbstractOpenWeatherProvider.java b/src/main/java/by/prominence/openweather/api/provider/AbstractOpenWeatherProvider.java index 5d65867..caaf63b 100644 --- a/src/main/java/by/prominence/openweather/api/provider/AbstractOpenWeatherProvider.java +++ b/src/main/java/by/prominence/openweather/api/provider/AbstractOpenWeatherProvider.java @@ -28,13 +28,10 @@ import by.prominence.openweather.api.exception.InvalidAuthTokenException; import by.prominence.openweather.api.model.Coordinates; import by.prominence.openweather.api.model.OpenWeatherResponse; import by.prominence.openweather.api.utils.JsonUtils; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.HttpClientBuilder; -import java.io.IOException; import java.lang.reflect.ParameterizedType; +import java.net.HttpURLConnection; +import java.net.URL; public abstract class AbstractOpenWeatherProvider implements OpenWeatherProvider { @@ -101,20 +98,16 @@ public abstract class AbstractOpenWeatherProvider url += "&type=" + accuracy; } - HttpClient httpClient = HttpClientBuilder.create().build(); - HttpGet request = new HttpGet(url); - HttpResponse httpResponse = null; + T openWeatherResponse = null; try { - httpResponse = httpClient.execute(request); - } catch (IOException e) { - e.printStackTrace(); - } + URL requestUrl = new URL(url); - T openWeatherResponse = null; - if (httpResponse != null) { - try { - openWeatherResponse = type.cast(JsonUtils.parseJson(httpResponse.getEntity().getContent(), type)); + HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection(); + connection.setRequestMethod("GET"); + + if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { + openWeatherResponse = type.cast(JsonUtils.parseJson(connection.getInputStream(), type)); if (openWeatherResponse.getResponseCode() == 401) { throw new InvalidAuthTokenException(); @@ -123,12 +116,11 @@ public abstract class AbstractOpenWeatherProvider if (openWeatherResponse.getResponseCode() == 404) { throw new DataNotFoundException(); } - - } catch (IOException e) { - e.printStackTrace(); - } catch (ClassCastException cce) { - cce.printStackTrace(); + } else { + throw new DataNotFoundException(); } + } catch (Exception ex) { + ex.printStackTrace(); } return openWeatherResponse; diff --git a/src/main/java/by/prominence/openweather/api/utils/JsonUtils.java b/src/main/java/by/prominence/openweather/api/utils/JsonUtils.java index c015cf6..4a00b3a 100644 --- a/src/main/java/by/prominence/openweather/api/utils/JsonUtils.java +++ b/src/main/java/by/prominence/openweather/api/utils/JsonUtils.java @@ -40,6 +40,8 @@ public class JsonUtils { result.append(line); } + reader.close(); + return JSON.parseObject(result.toString(), clazz); } }