Openweathermap org api java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Java Implementation of the OpenWeatherMap REST API

License

huysamen/openweathermap-java-api

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Читайте также:  white-space

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

OpenWeatherMap API Implementation (Java)

This library provides a Java implementation library to communicate with the OpenWeatherMap REST (version 2.5) API.

The following methods are available:

weatherByCityName(final String cityName); weatherByLatitudeLongitude(final float latitude, final float longitude); weatherByCityId(final int cityId); forecastByCityName(final String cityName); forecastByLatitudeLongitude(final float latitude, final float longitude); forecastByCityId(final int cityId); dailyForecastByCityName(final String cityName); dailyForecastByLatitudeLongitude(final float latitude, final float longitude); dailyForecastByCityId(final int cityId); searchByCityName(final String cityName, final Integer maxResults, final Accuracy accuracy); searchByLatitudeLongitude(final float latitude, final float longitude, final Integer maxResults, final Accuracy accuracy); 

About

Java Implementation of the OpenWeatherMap REST API

Источник

OpenWeatherMap Java

You can use the OpenWeatherMap API in Java using OkHttp or Unirest.

How to use the OpenWeatherMap API in Java (Java Example)

1. Sign up for a Free RapidAPI User Account

RapidAPI Free API Key

From any page on the RapidAPI Marketplace, click “Sign Up” and register for a free account.

2. Navigate the OpenWeatherMap API page

openweathermap api endpoints console rapidapi

Get to the OpenWeatherMap API Page by clicking here or searching for it in the RapidAPI marketplace search bar.

3. Subscribe to the API

openweathermap api pricing

Next, click on the API’s Pricing Tab and select a plan to subscribe to. The OpenWeatherMap API has 2 pricing plans on RapidAPI:

  1. Basic – OpenWeatherMap’s free plan. $0.00/month with a 100 request/day hard limit. Rate limited at 10 requests/minute.
  2. Pro – $10/month that offers unlimited API requests. Rate limited at 100 requests/minute.

4. Test an OpenWeatherMap API Endpoint

openweathermap API current weather data

After subscribing to a pricing plan, head back to the endpoints page and choose an endpoint, fill out the required parameters, and click “Test Endpoint”.

If done correctly, on the right side of the API console, you should see a response like this:

openweathermap api san francisco weather

5. Copy the Java (OkHttp or Unirest) Code Snippet and add it to your application!

rapidapi code snippets

Now that you have successfully tested that the API works, click on the Code Snippet dropdown and select one of the following:

You’ll see something similar to this:

OkHttp

OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://community-open-weather-map.p.rapidapi.com/weather?q=London%252Cuk") .get() .addHeader("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") .addHeader("x-rapidapi-key", "[your rapidapi key]") .build(); Response response = client.newCall(request).execute();

Unirest

HttpResponse response = Unirest.get("https://community-open-weather-map.p.rapidapi.com/weather?q=London%252Cuk") .header("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") .header("x-rapidapi-key", "[your rapidapi key]") .asString();

Modify as needed and drop it into your application.

What data is available with the OpenWeatherMap API?

The OMW API has 4 endpoints available on RapidAPI:

GET Current Weather Data

Description: Using this kind of requests you can get weather data in any location on the earth. The current weather data are updated online based on data from more than 40,000 weather stations.

OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://community-open-weather-map.p.rapidapi.com/weather?q=London%252Cuk") .get() .addHeader("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") .addHeader("x-rapidapi-key", "[your rapidapi key]") .build(); Response response = client.newCall(request).execute();

GET Call 16 day / daily forecast data

Description: 16-day forecasts are available for any location or city. Forecasts include daily weather and available in JSON or XML format. It is only available for all paid accounts.

OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://community-open-weather-map.p.rapidapi.com/forecast/daily?q=san%20francisco%252Cus") .get() .addHeader("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") .addHeader("x-rapidapi-key", "[your rapidapi key]") .build(); Response response = client.newCall(request).execute();

GET Search Weather Data

Description: By city name. Input the city name or its part and get the list of the most proper cities in the world. Example – Lon or Lond or London. The more precise city name you put the more precise list you will get. To make it more precise put the city’s name or its part, comma, the name of the county or 2-letter country code. You will get all the proper cities in the chosen county. The order is important – the first is city name then a comma then the county. Example – Lon, UK or Lon, GB or London, GB or Lon, England. By geographic coordinates.

OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://community-open-weather-map.p.rapidapi.com/find?units=imperial%252C%20metric&q=london") .get() .addHeader("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") .addHeader("x-rapidapi-key", "[your rapidapi key]") .build(); Response response = client.newCall(request).execute();

GET 5 day / 3 hour forecast data

Description: A 5-day forecast is available for any location or city. It includes weather data every 3 hours. The forecast is available in JSON or XML format.

OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("https://community-open-weather-map.p.rapidapi.com/forecast?q=san%20francisco%252Cus") .get() .addHeader("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") .addHeader("x-rapidapi-key", "[your rapidapi key]") .build(); Response response = client.newCall(request).execute();

Primary Sidebar

Build anything with APIs, faster.

Discover, evaluate, and integrate with any API. RapidAPI is the world’s largest API Hub with over 4 Million developers and 35,000 APIs.

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Guide to make a working Java program to get weather forecast, using OpenWeatherMap & API

hefnat/Weather

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Guide/Program to make a working Java program to get the weather forecast, using OpenWeatherMap & API.

Aside from json-jar library and your own API link, all in eclipse programmable. Getting API Link: get from OpenWeatherMap Homepage by registering there, free version is an option. Also, further guides are available on homepage of OpenWeatherMap.

In the following a couple of different uses will be listed:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import java.nio.charset.Charset;

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Locale;

import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject;

private static String readAll(Reader rd) throws IOException < StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) < sb.append((char) cp); >return sb.toString(); > public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException < InputStream is = new URL(url).openStream(); try < BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); return json; >finally < is.close(); >> public static void main(String[] args) throws IOException, JSONException < System.out .println("Today's Temperature: Please choose 1 for Zuerich, 2 for London, 3 for Paris, 4 for Rome or 5 for Boston."); System.out .println("7 days forecast: Please choose 11 for Zuerich, 22 for London, 33 for Paris, 44 for Rome or 55 for Boston."); System.out .println("Special options: 0 & -1"); Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); scanner.close(); int a = 1; int d = 0; JSONObject json = null; JSONObject json_specific = null; // get specific data in jsonobject variable Double result_temp = null; // get integer/double of temperature variable JSONArray json_list = null; // get array list of jsonarray variable JSONObject json_specific_day = null; // pick specific day variable out of list JSONObject json_city = null; //get city out of list String json_city_name = null; //get string of the prior picked city SimpleDateFormat df1 = new SimpleDateFormat("EEE", Locale.ENGLISH); //For only 3 initials of the day SimpleDateFormat df2 = new SimpleDateFormat("EEEE", Locale.ENGLISH); //Entire word/day as output Calendar c = Calendar.getInstance(); 

//example 1: One city & today's weather, case scenario

 if (t < 6 && t >0) < switch (t) < //the link in the following is listed multiple times except for the small difference of the desired city //in order: zuerich, london, paris, roma, boston case 1: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/weather?q=zuerich&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 2: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/weather?q=london&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 3: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/weather?q=paris&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 4: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/weather?q=roma&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 5: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/weather?q=boston&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; default: System.out .println("Please restart the program and be sure to choose one of the possible requests."); >json_specific = json.getJSONObject("main"); result_temp = json_specific.getDouble("temp"); c.add(Calendar.DATE, d); String output = df2.format(c.getTime()); System.out.println("\n"+"Today is "+ output+ " and the Temperature in "+json.get("name")+" is: "+result_temp); 

// Example 2: Display of whole week of a specific city

 > else if (t > 10 && t < 56) < switch (t) < case 11: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Zuerich&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 22: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 33: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Paris&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 44: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Roma&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 55: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Boston&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; default: System.out .println("Please restart the program and be sure to choose one of the possible requests."); >json_list = json.getJSONArray("list"); json_city = json.getJSONObject("city"); json_city_name = json_city.getString("name"); System.out.println("\n" + "The Forecast for " + json_city_name + " is: " + "\n"); for (int i = 0; i < 7; i++) < json_specific_day = json_list.getJSONObject(i); json_specific = json_specific_day.getJSONObject("temp"); result_temp = json_specific.getDouble("day"); // The following are the remnants of the output of the list I had to work through to get the desired data // System.out.println("The Complete List is " + json_list); // System.out.println("List of Weather of " + a + ". Day is " + json_specific_day); // System.out.println("Whole Temp Data of " + a + ". Day is " + json_specific); // Finally the desired forecast of the week of a specific city System.out.println("The Temperature of the " + a + ". Day is " + result_temp); a++; >

// Example 3: Forecast day & night for all listed cities and their week

 > else if (t == 0) < for (int i = 1; i < 6; i++) < switch (i) < case 1: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Zuerich&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 2: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 3: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Paris&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 4: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Roma&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 5: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Boston&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; >json_list = json.getJSONArray("list"); json_city = json.getJSONObject("city"); json_city_name = json_city.getString("name"); System.out.println("\n" + "The Forecast for " + json_city_name + " is: " + "\n"); for (int j = 0; j < 7; j++) < Double result_temp2 = null; json_specific_day = json_list.getJSONObject(j); json_specific = json_specific_day.getJSONObject("temp"); result_temp = json_specific.getDouble("day"); result_temp2 = json_specific.getDouble("night"); System.out.println(a + ". Day it's " + result_temp + " and at Night it's " + result_temp2+"."); a++; >a = 1; //following is a note of the list from which of the data "day" & "night" was picked // "temp":> > 

// Example 4: Basically example 3, but with weekdays initials included.

 >else if (t==-1) < for (int i = 1; i < 6; i++) < switch (i) < case 1: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Zuerich&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 2: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=London&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 3: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Paris&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 4: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Roma&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; case 5: json = readJsonFromUrl("http://api.openweathermap.org/data/2.5/forecast/daily?q=Boston&mode=json&units=metric&cnt=7&appid= add_your_API_code_here"); break; >json_list = json.getJSONArray("list"); json_city = json.getJSONObject("city"); json_city_name = json_city.getString("name"); System.out.println("\n" + "The Forecast for " + json_city_name + " is: " + "\n"); for (int j = 0; j < 7; j++) < Double result_temp2 = null; json_specific_day = json_list.getJSONObject(j); json_specific = json_specific_day.getJSONObject("temp"); result_temp = json_specific.getDouble("day"); result_temp2 = json_specific.getDouble("night"); c.add(Calendar.DATE, d); String output = df1.format(c.getTime()); System.out.println(output + ". it's " + result_temp + " and at Night it's " + result_temp2+"."); d++; >> > > 

About

Guide to make a working Java program to get weather forecast, using OpenWeatherMap & API

Источник

Оцените статью