A light OpenWeatherMap API for Java.
-
Either build the JAR or download it from the latest release
-
Create a folder, "lib", in the directory of a new/existing project
-
Move or copy the JAR file into the lib folder
-
Add the JAR file to the build path
IDEA: From the Project window, select and right-click the lib folder, then click Add as Library...
Eclipse: From the Package Explorer, select and right-click the JAR file, then select Build and click Add to Build Path
- In a Java source file, import the OpenWeatherMap API
import owmapi.OwmApi;
- Create an instance of the API
OwmApi owmApi = new OwmApi();
- Set the OpenWeatherMap App ID by passing it as a string
owmApi.setAppId("OWM-App-ID-Here");
- Get weather report based on zipcode
String weatherReport = owmApi.runWithZipcode("75080-3021");
import owmapi.OwmApi;
public class Main {
public static void main(String[] args) {
run("OWM-App-ID-Here", "75080-3021");
}
public static void run(String appId, String zipcode) {
OwmApi owmApi = new OwmApi();
owmApi.setAppId(appId);
String weatherReport = owmApi.runWithZipcode(zipcode);
if (weatherReport != null) {
System.out.println(weatherReport);
}
}
}
Result:
Weather forecast for Richardson city: cloudy and a temperature of 80.9 with a high of 84.2 and a low of 77.0
setAppId(String)
Sets the OpenWeatherMap App ID for retrieving data from the web API
runWithZip(String)
Runs the Java OpenWeatherMap API using zip code information
Note: This API includes more methods; however, this covers the majority of its functionality at this time.
All pull requests are welcome.