Question

I try to use weather api in asp.net mvc.

My code below:

        var url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=6b87pfhmjb7ydj6w596fujpu";
        var client = new HttpClient();

        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        var response = client.GetAsync(url).Result;

Reponse status is "ok".However i do not know how can i see result (as data or as xml )?

Any help will be appreciated.

Thanks.

Was it helpful?

Solution

You could create some ViewModels to deserializa it, for sample:

public class WeatherDesc
{
    public string value { get; set; }
}

public class WeatherIconUrl
{
    public string value { get; set; }
}

public class CurrentCondition
{
    public string cloudcover { get; set; }
    public string humidity { get; set; }
    public string observation_time { get; set; }
    public string precipMM { get; set; }
    public string pressure { get; set; }
    public string temp_C { get; set; }
    public string temp_F { get; set; }
    public string visibility { get; set; }
    public string weatherCode { get; set; }
    public List<WeatherDesc> weatherDesc { get; set; }
    public List<WeatherIconUrl> weatherIconUrl { get; set; }
    public string winddir16Point { get; set; }
    public string winddirDegree { get; set; }
    public string windspeedKmph { get; set; }
    public string windspeedMiles { get; set; }
}

public class Request
{
    public string query { get; set; }
    public string type { get; set; }
}

public class WeatherDesc2
{
    public string value { get; set; }
}

public class WeatherIconUrl2
{
    public string value { get; set; }
}

public class Weather
{
    public string date { get; set; }
    public string precipMM { get; set; }
    public string tempMaxC { get; set; }
    public string tempMaxF { get; set; }
    public string tempMinC { get; set; }
    public string tempMinF { get; set; }
    public string weatherCode { get; set; }
    public List<WeatherDesc2> weatherDesc { get; set; }
    public List<WeatherIconUrl2> weatherIconUrl { get; set; }
    public string winddir16Point { get; set; }
    public string winddirDegree { get; set; }
    public string winddirection { get; set; }
    public string windspeedKmph { get; set; }
    public string windspeedMiles { get; set; }
}

public class Data
{
    public List<CurrentCondition> current_condition { get; set; }
    public List<Request> request { get; set; }
    public List<Weather> weather { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
}

And to deserializa it, you could use the RootObject, for sample:

var response = client.GetStringAsync(url);
var rootObject = JsonConvert.DeserializeObject<RootObject >(response.Result);

OTHER TIPS

Something Like this., Just an another IDEA.

I just tried different Api call without any Post back to Server using Jquery to Get the Weather Report.

<script>
    $(document).ready(function () {
        $('#btnGetWeather').click(function () {
            $.post('http://api.openweathermap.org/data/2.5/weather?q=' + $('#txtCityName').val() + "," + $('#txtCountryCode').val(), function (data) {
                $('#lblTempMax').text(data.main.temp_max);
                $('#lblTempMin').text(data.main.temp_min);
                $('#lblSunRise').text(msToTime(data.sys.sunrise));
                $('#lblSunSet').text(msToTime(data.sys.sunset));
            });

            return false;
        });

        function msToTime(s) {
            var milli = s * 1000;
            return new Date(milli);
        }
    });
</script>

Try changing the GetAsync to

var response = await httpClient.GetStringAsync(uri);

That should get you the response data as a string and if it's json you'll just need to parse it. You need to use the await keyword so that the execution of your method is suspended until the httpClient returns a result.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top