Got rid of 'httpclient' dependency.

This commit is contained in:
Prominence 2018-07-07 21:29:58 +03:00
parent 17fb63bd69
commit a9ce72ba41
3 changed files with 15 additions and 30 deletions

View File

@ -8,15 +8,7 @@
<artifactId>by.prominence.openweater.api</artifactId> <artifactId>by.prominence.openweater.api</artifactId>
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<dependencies> <dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson</artifactId>
@ -24,5 +16,4 @@
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -28,13 +28,10 @@ import by.prominence.openweather.api.exception.InvalidAuthTokenException;
import by.prominence.openweather.api.model.Coordinates; import by.prominence.openweather.api.model.Coordinates;
import by.prominence.openweather.api.model.OpenWeatherResponse; import by.prominence.openweather.api.model.OpenWeatherResponse;
import by.prominence.openweather.api.utils.JsonUtils; 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.lang.reflect.ParameterizedType;
import java.net.HttpURLConnection;
import java.net.URL;
public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse> implements OpenWeatherProvider { public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse> implements OpenWeatherProvider {
@ -101,20 +98,16 @@ public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse>
url += "&type=" + accuracy; url += "&type=" + accuracy;
} }
HttpClient httpClient = HttpClientBuilder.create().build(); T openWeatherResponse = null;
HttpGet request = new HttpGet(url);
HttpResponse httpResponse = null;
try { try {
httpResponse = httpClient.execute(request); URL requestUrl = new URL(url);
} catch (IOException e) {
e.printStackTrace();
}
T openWeatherResponse = null; HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
if (httpResponse != null) { connection.setRequestMethod("GET");
try {
openWeatherResponse = type.cast(JsonUtils.parseJson(httpResponse.getEntity().getContent(), type)); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
openWeatherResponse = type.cast(JsonUtils.parseJson(connection.getInputStream(), type));
if (openWeatherResponse.getResponseCode() == 401) { if (openWeatherResponse.getResponseCode() == 401) {
throw new InvalidAuthTokenException(); throw new InvalidAuthTokenException();
@ -123,12 +116,11 @@ public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse>
if (openWeatherResponse.getResponseCode() == 404) { if (openWeatherResponse.getResponseCode() == 404) {
throw new DataNotFoundException(); throw new DataNotFoundException();
} }
} else {
} catch (IOException e) { throw new DataNotFoundException();
e.printStackTrace();
} catch (ClassCastException cce) {
cce.printStackTrace();
} }
} catch (Exception ex) {
ex.printStackTrace();
} }
return openWeatherResponse; return openWeatherResponse;

View File

@ -40,6 +40,8 @@ public class JsonUtils {
result.append(line); result.append(line);
} }
reader.close();
return JSON.parseObject(result.toString(), clazz); return JSON.parseObject(result.toString(), clazz);
} }
} }