diff --git a/src/main/java/HWLesson_6/WeatherApp.java b/src/main/java/HWLesson_6/WeatherApp.java new file mode 100644 index 0000000..fd54863 --- /dev/null +++ b/src/main/java/HWLesson_6/WeatherApp.java @@ -0,0 +1,50 @@ +package HWLesson_6; + +import java.io.IOException; +import java.util.Objects; + +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; + +public class WeatherApp { + private static final String BASE_HOST = "dataservice.accuweather.com"; + private static final String FORECAST = "forecasts"; + private static final String API_VERSION = "v1"; + private static final String FORECAST_TYPE = "daily"; + private static final String FORECAST_PERIOD = "5day"; + private static final String SAINT_PETERSBURG_KEY = "295212"; + private static final String API_KEY = "NMhWOUAJY8cgKzdgm5fBqcOsGluxsD5T"; + + + + public static void main(String[] args) throws IOException { + + OkHttpClient client = new OkHttpClient(); + HttpUrl url = new HttpUrl.Builder() + .scheme("http") + .host(BASE_HOST) + .addPathSegment(FORECAST) + .addPathSegment(API_VERSION) + .addPathSegment(FORECAST_TYPE) + .addPathSegment(FORECAST_PERIOD) + .addPathSegment(SAINT_PETERSBURG_KEY) + .addQueryParameter("apikey", API_KEY) + .addQueryParameter("language", "ru-ru") + .addQueryParameter("metric", "true") + .build(); + + System.out.println(url); + + Request requesthttp = new Request.Builder() + .addHeader("accept", "application/json") + .url(url) + .build(); + + String jsonResponse = Objects.requireNonNull(client.newCall(requesthttp).execute().body()).string(); + System.out.println(jsonResponse); + + + } + +}