Monday 12 June 2017

Implement open Weather Api using C# using the windows application

Tags

 you can implement this weather API
against City or the against the Lattitude and Longitude

while designing  I used  eight labels to show the Values of Respected JSON
first u need to install NuGet package using package manager console
 Install-Package Newtonsoft.Json
 private void Frm_WeatherReport_Load(object sender, EventArgs e)
        {
string latitide = "19.9975";
            string longitude = "73.7898";

            const string apikey = "You nee
d to put your key here ...";
     
                using (WebClient wcumum = new WebClient())
                {
                    try
                    {

                        // Ag city
                       // string request = string.Format("http://api.openweathermap.org/data/2.5/weather?q={0}&appid={1}&units=metric", textBox1.Text, apikey);
                   
                        String request = "http://api.openweathermap.org/data/2.5/weather?lat=" + latitide + "&lon=" + longitude + "&appid=f63af5e4778e517ec5e7599d6172eae5";
                        var jsonres = wcumum.DownloadString(request);
                        var objectres = JsonConvert.DeserializeObject<OpenWeatherMap.Root>(jsonres);
               
                        OpenWeatherMap.Root resultobj = objectres;
                 

                        string ctry = string.Format("{0}", resultobj.sys.country);
                        string cty = string.Format( resultobj.name);
                        string lttd = string.Format("{0}", resultobj.coord.lat);
                        string lngtd = string.Format("{0}", resultobj.coord.lon);
                        string tmprtr = string.Format("{0} C", resultobj.main.temp);
                        string wthr = string.Format("{0}:{1}", resultobj.weather[0].main, resultobj.weather[0].description);
                        string hmdty = string.Format("{0}", resultobj.main.humidity);
                        string prssr = string.Format("{0}", resultobj.main.pressure);
                        string wind = string.Format("{0}", resultobj.wind.speed);

                         wind = string.Format("{0}", resultobj.wind.speed);

                         lcity.Text = cty + ", " + ctry;
                        ltemp.Text = (float.Parse(tmprtr.ToString().Remove(tmprtr.Length - 2)) - 273.15).ToString().Substring(0, 5) + " °C";
                        lwind.Text = wind+" mph";
                        lhumidity.Text = hmdty+ " %";
                        lpressure.Text = prssr+" hpa";
                 
                        lweather.Text = wthr;
                    }
                    catch (Exception exc)
                    {
                        XtraMessageBox.Show("Please enter correct lattitude and langitude.", "Weather Master", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.Close();
                    }
                }









 public class OpenWeatherMap
    {
        public class Coord
        {
            public double lon { get; set; }
            public double lat { get; set; }
        }

        public class Sys
        {
            public int type { get; set; }
            public int id { get; set; }
            public double message { get; set; }
            public string country { get; set; }
            public int sunrise { get; set; }
            public int sunset { get; set; }

        }

        public class Weather
        {
            public int id { get; set; }
            public string main { get; set; }
            public string description { get; set; }
            public string icon { get; set; }
        }

        public class Main
        {
            public double temp { get; set; }
            public int humidity { get; set; }
            public double pressure { get; set; }
            public double temp_min { get; set; }
            public double temp_max { get; set; }
        }

        public class Wind
        {
            public double speed { get; set; }
            public double gust { get; set; }
            public double deg { get; set; }
        }

        public class Clouds
        {
            public int all { get; set; }
        }

        public class Root
        {
            public Coord coord { get; set; }
            public Sys sys { get; set; }
            public List<Weather> weather { get; set; }
            public string @base { get; set; }
            public Main main { get; set; }
            public Wind wind { get; set; }
            public Dictionary<string, double> rain { get; set; }
            public Clouds clouds { get; set; }
            public int dt { get; set; }
            public int id { get; set; }
            public string name { get; set; }
            public int cod { get; set; }
        }

This Is The Newest Post


EmoticonEmoticon