Accessing data for multiple days from weather api

I have the program working where it displays the data for the current day but I not to sure how to get the weather data for the following days. Do I have to first send the json results to an array and cycle through?
9 Replies
[ECH]JamHighlight
Like where I add adding the icon [weather][0] do I have to use a loop to go through the index
Sebastian
Sebastian4w ago
You probably need to call a different API endpoint See the docs here https://openweathermap.org/api
Weather API
Explore OpenWeather's vast range of weather APIs including the versatile One Call API 3.0. Ideal for both beginners and professionals, our APIs offer current weather, minute-by-minute forecasts, historical data archives, and future predictions. Access weather data starting from 01-01-1979, global weather maps, solar irradiance predictions, air p...
[ECH]JamHighlight
api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={API key} Ye i had to use this one instead
Sebastian
Sebastian4w ago
Also remove the API key from your message and keep it private
Sebastian
Sebastian4w ago
Here are some additional pointers: 1. Move the API key out of your code entirely, load it from a local configuration file. Make sure that the configuration file is not tracked by a versioning system (so if you are using git, add it to .gitignore) 2. Don't initialize HttpClient repeatedly. This is especially bad with this type. 3. Always return early if you can (google "c# return early" if you don't know what I mean) 4. You were checking whether the HttpClient was null right after creating it (so it should never be null, the condition is useless) 5. Ideally you should replace JObject with something else 6. You forgot to dispose the object in response variable
private const string ApiKey = "..."; // TODO Load from configuration file instead, keeping this in code is a bad idea

private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
string city = Citysearch.Text;
string url = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={ApiKey}";

try
{
using HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string content = await response.ReadAsStringAsync();
if (content is null)
{
Console.WriteLine("Received no content from wheather API");
return;
}

JObject weatherData = JObject.Parse(content);
// Handle weather data
}
catch (Exception ex)
{
Console.WriteLine($"Error while fetching from weather API: {ex.Message}");
}
}
private const string ApiKey = "..."; // TODO Load from configuration file instead, keeping this in code is a bad idea

private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
string city = Citysearch.Text;
string url = $"https://api.openweathermap.org/data/2.5/forecast?lat={lat}&lon={lon}&appid={ApiKey}";

try
{
using HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string content = await response.ReadAsStringAsync();
if (content is null)
{
Console.WriteLine("Received no content from wheather API");
return;
}

JObject weatherData = JObject.Parse(content);
// Handle weather data
}
catch (Exception ex)
{
Console.WriteLine($"Error while fetching from weather API: {ex.Message}");
}
}
How to deserialize JSON in C# - .NET
Learn how to use the System.Text.Json namespace to deserialize from JSON in .NET. Includes sample code.
Scratch
Scratch4w ago
@[ECH]JamHighlight I deleted the message with your code, you leaked your api key. I recommend you rotate it.
[ECH]JamHighlight
ik u said dont make http client but like but How do I get the icon to work without doing it twice
Sebastian
Sebastian3w ago
Create the instance once and then reuse it Is this in winforms or? Well, regardless. You should be able to create an instance of HttpClient and save the reference to a field/property of the class you are working in. For example:
class MyProgram
{
private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
// Use 'client' as many times as you need
}
}
class MyProgram
{
private readonly HttpClient client = new();

async void FetchWeatherForecast()
{
// Use 'client' as many times as you need
}
}
This is as opposed to what (if I remember correctly) you had before:
class MyProgram
{
async void FetchWeatherForecast()
{
using var client = new HttpClient(); // Not good!
}
}
class MyProgram
{
async void FetchWeatherForecast()
{
using var client = new HttpClient(); // Not good!
}
}
To see why this is important (for HttpClient specifically) look here (if interested): https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-net-http-httpclient#instancing
[ECH]JamHighlight
ye its in win forms I was hoping to avoid having to use the client at all because ill have to repeat code over and ovr

Did you find this page helpful?