From 9b19742464825ee5e865bdfe33f5592dda8a7f7f Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Thu, 3 Jun 2021 21:39:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D1=8C?= =?UTF-8?q?=D1=82=D0=B5,=20=D0=BF=D0=BE=D0=B6=D0=B0=D0=BB=D1=83=D0=B9?= =?UTF-8?q?=D1=81=D1=82=D0=B0,=20=D0=94=D0=976?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/HWLesson_6/WeatherApp.java | 50 ++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/main/java/HWLesson_6/WeatherApp.java 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); + + + } + +}