#37 java: open weather api ex

This commit is contained in:
haerong22
2023-05-23 21:04:33 +09:00
parent 35d8416f44
commit 3747851cff
2 changed files with 58 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ repositories {
dependencies {
implementation 'org.jsoup:jsoup:1.16.1'
implementation 'com.google.code.gson:gson:2.10.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'

View File

@@ -0,0 +1,57 @@
package openweather;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class WeatherEx {
public static void main(String[] args) {
final String apiKey = args[0];
final String city = "Seoul";
final String unit = "metric";
final StringBuilder sb = new StringBuilder();
sb.append("https://api.openweathermap.org/data/2.5/weather");
sb.append("?q=").append(city);
sb.append("&appid=").append(apiKey);
sb.append("&unit=").append(unit);
try {
final URL url = new URL(sb.toString());
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", "application/json");
final int responseCode = connection.getResponseCode();
if (responseCode == 200) {
final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
final StringBuilder content = new StringBuilder();
while ((inputLine=in.readLine()) != null) {
content.append(inputLine);
}
in.close();
System.out.println("content = " + content);
JsonObject weatherData = JsonParser.parseString(content.toString()).getAsJsonObject();
JsonObject mainData = weatherData.getAsJsonObject("main");
double temp = mainData.get("temp").getAsDouble();
System.out.println("temp = " + temp);
connection.disconnect();
} else {
System.out.println("responseCode = " + responseCode);
System.out.println("responseMessage = " + connection.getResponseMessage());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}