mirror of
https://github.com/Prominence/openweathermap-java-api.git
synced 2026-01-10 11:56:44 +03:00
Got rid of 'httpclient' dependency.
This commit is contained in:
parent
17fb63bd69
commit
a9ce72ba41
9
pom.xml
9
pom.xml
@ -8,15 +8,7 @@
|
||||
<artifactId>by.prominence.openweater.api</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
|
||||
<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>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
@ -24,5 +16,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
</project>
|
||||
@ -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<T extends OpenWeatherResponse> implements OpenWeatherProvider {
|
||||
|
||||
@ -101,20 +98,16 @@ public abstract class AbstractOpenWeatherProvider<T extends OpenWeatherResponse>
|
||||
url += "&type=" + accuracy;
|
||||
}
|
||||
|
||||
HttpClient httpClient = HttpClientBuilder.create().build();
|
||||
HttpGet request = new HttpGet(url);
|
||||
HttpResponse httpResponse = null;
|
||||
|
||||
try {
|
||||
httpResponse = httpClient.execute(request);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
T openWeatherResponse = null;
|
||||
if (httpResponse != null) {
|
||||
|
||||
try {
|
||||
openWeatherResponse = type.cast(JsonUtils.parseJson(httpResponse.getEntity().getContent(), type));
|
||||
URL requestUrl = new URL(url);
|
||||
|
||||
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<T extends OpenWeatherResponse>
|
||||
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;
|
||||
|
||||
@ -40,6 +40,8 @@ public class JsonUtils {
|
||||
result.append(line);
|
||||
}
|
||||
|
||||
reader.close();
|
||||
|
||||
return JSON.parseObject(result.toString(), clazz);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user