feat: helloworld weatherapi
This commit is contained in:
parent
dc5ca236d0
commit
c538ecc7ad
@ -12,8 +12,16 @@ import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
String APIKey = args[0];
|
||||
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 {
|
||||
@ -29,4 +37,21 @@ public class Main {
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user