✅ 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.
17 Replies
Angius
Angius2mo ago
What exception? Also, what kind of an app is it? WPF? Winforms?
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
Catch (Exception ex) Winforms
Angius
Angius2mo ago
Yeah, aight, that's where the exception occured But what exception?
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
Exception thrown: 'Newtonsoft.Json.JsonReaderException' in Newtonsoft.Json.dll
Angius
Angius2mo ago
Meaning something's off with the JSON Use the debugger to check that Could be that you're not getting JSON at all, or that it differs from the class you're trying to deserialize it to
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
I was wondering if its my apiurl maybe
Jimmacle
Jimmacle2mo ago
if that's really your API key you should remove it from here and rotate it
Angius
Angius2mo ago
You can stop wondering and gain certainty after seeing what gets returned from the API
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
Im sure this discord is full of kind individuals who arent weird
Jimmacle
Jimmacle2mo ago
it's a public discord, and on top of that these threads are published on the internet
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
It just keeps throwing me the exception
Jimmacle
Jimmacle2mo ago
you need to remove the json stuff and just look at the raw response or make the request in postman or something
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
Ok So its working things are going wrong when I try to convert it
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await client.GetStringAsync(apiUrl);
if (json != null)
{
Debug.WriteLine($"json {json}");
}
else
{
Debug.WriteLine("empty");
}
}
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await client.GetStringAsync(apiUrl);
if (json != null)
{
Debug.WriteLine($"json {json}");
}
else
{
Debug.WriteLine("empty");
}
}
Angius
Angius2mo ago
So what are you getting from this call?
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
What I want the weather details just not formated I just have to find a way to convert it now I saw a post saying u cannot update ui from a background thread could that of been causing the issue?
Jimmacle
Jimmacle2mo ago
no, you're getting a JsonReaderException which means the error is related to json serialization the actual details in that exception are needed to get more specific about what's going wrong
[ECH]JamHighlight
[ECH]JamHighlightOP2mo ago
U right u right Alr I got it working I just had lines that weren't needed
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await client.GetStringAsync(apiUrl);
if (json != null)
{
JObject weatherdata = JObject.Parse(json);
tempinfo.Text = weatherdata["main"]["temp"].ToString();
Debug.WriteLine($"json {json}");

}
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await client.GetStringAsync(apiUrl);
if (json != null)
{
JObject weatherdata = JObject.Parse(json);
tempinfo.Text = weatherdata["main"]["temp"].ToString();
Debug.WriteLine($"json {json}");

}
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())
{
if (client == null)
{
Debug.WriteLine("No client");
}
else

{
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await client.GetStringAsync(apiUrl);
if (json != null)
{
JObject weatherdata = JObject.Parse(json);
tempinfo.Text = weatherdata["main"]["temp"].ToString();
windspeedinfo.Text = weatherdata["wind"]["speed"].ToString();
humidityinfo.Text = weatherdata["main"]["humidity"].ToString();
cityinfo.Text = city;
countryinfo.Text = weatherdata["sys"]["country"].ToString();

Debug.WriteLine($"json {json}");

}
else
{
Debug.WriteLine("empty");
}
}
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())
{
if (client == null)
{
Debug.WriteLine("No client");
}
else

{
try
{
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await client.GetStringAsync(apiUrl);
if (json != null)
{
JObject weatherdata = JObject.Parse(json);
tempinfo.Text = weatherdata["main"]["temp"].ToString();
windspeedinfo.Text = weatherdata["wind"]["speed"].ToString();
humidityinfo.Text = weatherdata["main"]["humidity"].ToString();
cityinfo.Text = city;
countryinfo.Text = weatherdata["sys"]["country"].ToString();

Debug.WriteLine($"json {json}");

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

}
}
}
Completed for those who may need in the future.

Did you find this page helpful?