feat: helloworld weatherapi

This commit is contained in:
Nemo D'ACREMONT 2024-11-01 08:47:50 +01:00
parent dc5ca236d0
commit c538ecc7ad
No known key found for this signature in database
GPG Key ID: 6E5BCE8022FA8276

View File

@ -12,8 +12,16 @@ import java.util.Arrays;
public class Main { public class Main {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
String APIKey = args[0];
System.out.println("Args: " + Arrays.toString(args)); System.out.println("Args: " + Arrays.toString(args));
System.out.println(fetchChuckNorrisJoke());
JSONObject bordeauxWeather = fetchBordeauxWeather(APIKey);
System.out.println("Bordeaux Weather:");
System.out.println(
"\tTemp: "
+ bordeauxWeather.getJSONObject("current").getFloat("feelslike_c")
+ "°C"
);
} }
public static JSONObject fetchChuckNorrisJoke() throws IOException { public static JSONObject fetchChuckNorrisJoke() throws IOException {
@ -29,4 +37,21 @@ public class Main {
} }
return new JSONObject(result.toString()); return new JSONObject(result.toString());
} }
public static JSONObject fetchBordeauxWeather(String WeatherAPIKey) throws IOException {
StringBuilder result = new StringBuilder();
URL url = URI.create(
"https://api.weatherapi.com/v1/current.json?q=Bordeaux&key="
+ WeatherAPIKey
).toURL();
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(conn.getInputStream()))) {
for (String line; (line = reader.readLine()) != null; ) {
result.append(line);
}
}
return new JSONObject(result.toString());
}
} }