© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
35 replies
[ECH]JamHighlight[100 vouches]

✅ Exception being thrown when attempting to use Weather api

 async void callweatherapi()
 {
     string apiKey = "kanyecooked";
     string city = Citysearch.Text;
     string apiUrl = $"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={apiKey}&units=metric";
     using (HttpClient client = new HttpClient())
     {
         try
         {
             HttpResponseMessage response = await client.GetAsync(apiUrl);
             response.EnsureSuccessStatusCode();
             string json = await client.GetStringAsync(apiUrl);
             WeatherConditions.Rootobject weatherdetails = JsonConvert.DeserializeObject<WeatherConditions.Rootobject>(json);
             forcastinfo.Text = weatherdetails.weather[0].main;
             windspeedinfo.Text = weatherdetails.wind.speed.ToString();

             if(weatherdetails == null)
             {
                 Debug.WriteLine("no data");
             }

         }
         catch (Exception ex)
         {
             Console.WriteLine("Did not work"+ex.Message);
         }

     }
 }
 async void callweatherapi()
 {
     string apiKey = "kanyecooked";
     string city = Citysearch.Text;
     string apiUrl = $"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={apiKey}&units=metric";
     using (HttpClient client = new HttpClient())
     {
         try
         {
             HttpResponseMessage response = await client.GetAsync(apiUrl);
             response.EnsureSuccessStatusCode();
             string json = await client.GetStringAsync(apiUrl);
             WeatherConditions.Rootobject weatherdetails = JsonConvert.DeserializeObject<WeatherConditions.Rootobject>(json);
             forcastinfo.Text = weatherdetails.weather[0].main;
             windspeedinfo.Text = weatherdetails.wind.speed.ToString();

             if(weatherdetails == null)
             {
                 Debug.WriteLine("no data");
             }

         }
         catch (Exception ex)
         {
             Console.WriteLine("Did not work"+ex.Message);
         }

     }
 }

This method is what I'm using to try and use the weather api. Im not to sure what I did wrong since there are no errors.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Exception when attempting to use keyed dependencies
C#CC# / help
3y ago
TypeInitializationException thrown by runtime when attempting to use JsonSerializer (.NET 8.0).
C#CC# / help
3y ago
❔ Weather API
C#CC# / help
4y ago
❔ NullReference exception thrown when assigning a value to a decimal??
C#CC# / help
4y ago