نمایش آب و هوای روزانه توسط Weather Api در Asp.Net
چهارشنبه 19 فروردین 1394در این مقاله آب و هوای روزانه توسط Weather Api در Asp.Net را نمایش خواهیم داد
در این مقاله آب و هوای روزانه توسط Weather Api در Asp.Net را نمایش خواهیم داد:
Weather API یک وب سرویس است که نام شهر یا Zip Code را میگیرد و اطلاعات را به فرمت Json برای نمایش در صفحه وب بر می گرداند این وب سرویس به ما اجازه می دهد درخواست رایگان تا حد معینی داشته باشیم ما بنیاز به ثبت نام و گرفتن API Key (AppId) داریم که میتوان از لینک زیراین عمل را انجام دهید.
یک پروژه جدید از نوع وب فرم بسازید سپس یک صفحه جدید به پروژه خود اضافه کرده و از جعبه ابزار یک کنترل TextBox، یک دکمه ،label ،جدول و یک کنترل image اضافه کنید:
<asp:TextBox ID="txtCity" runat="server" Text="" /> <asp:Button Text="اطلاعات آب و هوا" runat="server" OnClick="GetWeatherInfo" /> <hr /> <table id="tblWeather" runat="server" border="0" cellpadding="0" cellspacing="0" visible="false"> <tr> <th colspan="2"> اطلاعات آب و هوا </th> </tr> <tr> <td rowspan="3"> <asp:Image ID="imgWeatherIcon" runat="server" /> </td> </tr> <tr> <td> <asp:Label ID="lblCity_Country" runat="server" /> <asp:Image ID="imgCountryFlag" runat="server" /> <asp:Label ID="lblDescription" runat="server" /> Humidity: <asp:Label ID="lblHumidity" runat="server" /> </td> </tr> <tr> <td> دما: (حداقل: <asp:Label ID="lblTempMin" runat="server" /> حداکثر: <asp:Label ID="lblTempMax" runat="server" /> روز: <asp:Label ID="lblTempDay" runat="server" /> شب: <asp:Label ID="lblTempNight" runat="server" />) </td> </tr> </table>
برای نگه داشتن اطلاعاتی که json از وب سرویس Weather API برمیگرداند ما نیاز به کلاس های زیر با خصوصیاتشان داریم:
public class WeatherInfo { public City city { get; set; } public List<List> list { get; set; } } public class City { public string name { get; set; } public string country { get; set; } } public class Temp { public double day { get; set; } public double min { get; set; } public double max { get; set; } public double night { get; set; } } public class Weather { public string description { get; set; } public string icon { get; set; } } public class List { public Temp temp { get; set; } public int humidity { get; set; } public List<Weather> weather { get; set; } }
زمانی که روی دکمه اطلاعات آب و هوا کلیک میشود نام شهر یا Zip Code همراه با API Key (AppId) با استفاده از کلاس WebClient به وب سرویس Weather API پاس داده میشود سپس وب سرویس Weather API اطلاعات آب و هوای روزانه را به فرمت Json باز می گرداند.رشته جیسون deserialized شده برای خصوصیت شی کلاس و درنهایت جزئیات نمایش داده خواهند شد در فیلد های مربوطه در جدول:
protected void GetWeatherInfo(object sender, EventArgs e) { string appId = "<App Id>"; string url = string.Format("http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&units=metric&cnt=1&APPID={1}", txtCity.Text.Trim(), appId); using (WebClient client = new WebClient()) { string json = client.DownloadString(url); WeatherInfo weatherInfo = (new JavaScriptSerializer()).Deserialize<WeatherInfo>(json); lblCity_Country.Text = weatherInfo.city.name + "," + weatherInfo.city.country; imgCountryFlag.ImageUrl = string.Format("http://openweathermap.org/images/flags/{0}.png", weatherInfo.city.country.ToLower()); lblDescription.Text = weatherInfo.list[0].weather[0].description; imgWeatherIcon.ImageUrl = string.Format("http://openweathermap.org/img/w/{0}.png", weatherInfo.list[0].weather[0].icon); lblTempMin.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.min, 1)); lblTempMax.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.max, 1)); lblTempDay.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.day, 1)); lblTempNight.Text = string.Format("{0}°С", Math.Round(weatherInfo.list[0].temp.night, 1)); lblHumidity.Text = weatherInfo.list[0].humidity.ToString(); tblWeather.Visible = true; } }
- ASP.net
- 6k بازدید
- 4 تشکر