我想创建java天气应用程序。
我有Open Weather的代码和API,但我没有jar文件。
这是我的代码
public class WeatherTest {
public static final void main(String[] args) {
boolean isMetric = true;
String owmApiKey = "XXXXXXXXXXXX";
/* YOUR OWM API KEY HERE */
String weatherCity = "Brisbane,AU";
byte forecastDays = 3;
OpenWeatherMap.Units units = (isMetric)
? OpenWeatherMap.Units.METRIC
: OpenWeatherMap.Units.IMPERIAL;
OpenWeatherMap owm = new OpenWeatherMap(units, owmApiKey);
try {
DailyForecast forecast = owm.dailyForecastByCityName(weatherCity, forecastDays);
System.out.println("Weather for: " + forecast.getCityInstance().getCityName());
int numForecasts = forecast.getForecastCount();
for (int i = 0; i < numForecasts; i++) {
DailyForecast.Forecast dayForecast = forecast.getForecastInstance(i);
DailyForecast.Forecast.Temperature temperature = dayForecast.getTemperatureInstance();
System.out.println("\t" + dayForecast.getDateTime());
System.out.println("\tTemperature: " + temperature.getMinimumTemperature()
+ " to " + temperature.getMaximumTemperature() + "\n");
}
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:2)
OpenWeatherMap提供了一个REST API,您可以调用它来获取天气数据。有关API规范,请参阅https://openweathermap.org/api。
为了使用Java代码调用它,您必须实现REST Java客户端。有很多方法可以做到这一点,但首先,您可以参考https://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/
或者,有许多客户开发了围绕OpenWeatherMap API。您可以在https://github.com/search?p=1&q=openweathermap&ref=cmdform&type=Repositories
进行搜索由于您正在寻找Java,我建议您查看https://github.com/xSAVIKx/openweathermap-java-api。
,可以使用此示例