Exemplo n.º 1
0
        public CurrentWeather(string apiKey, string queryStr, UnitsEnum units)
        {
            string unitsString;

            switch (units)
            {
            case UnitsEnum.Metric: { unitsString = "metric"; break; }

            case UnitsEnum.Imperial: { unitsString = "imperial"; break; }

            default: { unitsString = "standard"; break; }
            }

            JObject jsonData;

            using (var client = new System.Net.WebClient())
                jsonData = JObject.Parse(client.DownloadString($"http://api.openweathermap.org/data/2.5/weather?appid={apiKey}&q={queryStr}&units={unitsString}"));

            if (jsonData.SelectToken("cod").ToString() == "200")
            {
                ValidRequest = true;
                Coord        = new Coord(jsonData.SelectToken("coord"));
                foreach (JToken weather in jsonData.SelectToken("weather"))
                {
                    Weathers.Add(new Weather(weather));
                }
                Base = jsonData.SelectToken("base").ToString();
                Main = new Main(jsonData.SelectToken("main"));
                if (jsonData.SelectToken("visibility") != null)
                {
                    Visibility = double.Parse(jsonData.SelectToken("visibility").ToString(), CultureInfo.InvariantCulture);
                }
                Wind = new Wind(jsonData.SelectToken("wind"));
                if (jsonData.SelectToken("rain") != null)
                {
                    Rain = new Rain(jsonData.SelectToken("rain"));
                }
                if (jsonData.SelectToken("snow") != null)
                {
                    Snow = new Snow(jsonData.SelectToken("snow"));
                }
                Clouds = new Clouds(jsonData.SelectToken("clouds"));
                Sys    = new Sys(jsonData.SelectToken("sys"));
                ID     = int.Parse(jsonData.SelectToken("id").ToString(), CultureInfo.InvariantCulture);
                Name   = jsonData.SelectToken("name").ToString();
                Cod    = int.Parse(jsonData.SelectToken("cod").ToString(), CultureInfo.InvariantCulture);
            }
            else
            {
                ValidRequest = false;
            }
        }
Exemplo n.º 2
0
        public Query(string apiKey, string queryStr)
        {
            string jSonString = "";

            try
            {
                jSonString = new System.Net.WebClient().DownloadString(string.Format("http://api.openweathermap.org/data/2.5/weather?appid={0}&q={1}", apiKey, queryStr));
            }
            catch (System.Net.WebException) {; }

            if (jSonString != "")
            {
                JObject jsonData = JObject.Parse(jSonString);
                if (jsonData.SelectToken("cod").ToString() == "200")
                {
                    validRequest = true;
                    coord        = new Coord(jsonData.SelectToken("coord"));
                    foreach (JToken weather in jsonData.SelectToken("weather"))
                    {
                        weathers.Add(new Weather(weather));
                    }
                    baseStr = jsonData.SelectToken("base").ToString();
                    main    = new Main(jsonData.SelectToken("main"));
                    if (jsonData.SelectToken("visibility") != null)
                    {
                        visibility = double.Parse(jsonData.SelectToken("visibility").ToString());
                    }
                    wind = new Wind(jsonData.SelectToken("wind"));
                    if (jsonData.SelectToken("rain") != null)
                    {
                        rain = new Rain(jsonData.SelectToken("rain"));
                    }
                    if (jsonData.SelectToken("snow") != null)
                    {
                        snow = new Snow(jsonData.SelectToken("snow"));
                    }
                    clouds = new Clouds(jsonData.SelectToken("clouds"));
                    sys    = new Sys(jsonData.SelectToken("sys"));
                    id     = int.Parse(jsonData.SelectToken("id").ToString());
                    name   = jsonData.SelectToken("name").ToString();
                    cod    = int.Parse(jsonData.SelectToken("cod").ToString());
                }
                else
                {
                    validRequest = false;
                }
            }
            else
            {
                validRequest = false;
            }
        }
Exemplo n.º 3
0
        public Query(string apiKey, string queryStr)
        {
            Weathers = new List <Weather>();

            JObject jsonData = JObject.Parse(new System.Net.WebClient().DownloadString(string.Format(Query.BaseAdress + "weather?{0}&appid={1}", queryStr, apiKey)));

            //JObject jsonData = JObject.Parse(new System.Net.WebClient().DownloadString(string.Format(Query.BaseAdress + "weather?q={0}&appid={1}", queryStr, apiKey)));
            if (jsonData.SelectToken(COD).ToString() == Query.ValidCod)
            {
                ValidRequest = true;
                Coord        = new Coord(jsonData.SelectToken(CoordSelector));

                foreach (JToken weather in jsonData.SelectToken(WeatherSelector))
                {
                    Weathers.Add(new Weather(weather));
                }

                Base = jsonData.SelectToken(BaseSelector).ToString();
                Main = new Main(jsonData.SelectToken(MainSelector));

                if (jsonData.SelectToken(VisibilitySelector) != null)
                {
                    Visibility = double.Parse(jsonData.SelectToken(VisibilitySelector).ToString());
                }

                Wind = new Wind(jsonData.SelectToken(WindSelector));

                if (jsonData.SelectToken(RaidSelector) != null)
                {
                    Rain = new Rain(jsonData.SelectToken(RaidSelector));
                }

                if (jsonData.SelectToken(SnowSelector) != null)
                {
                    Snow = new Snow(jsonData.SelectToken(SnowSelector));
                }

                Clouds = new Clouds(jsonData.SelectToken(CloudsSelector));
                Sys    = new Sys(jsonData.SelectToken(SysSelector));
                ID     = int.Parse(jsonData.SelectToken(IdSelector).ToString());
                Name   = jsonData.SelectToken(NameSelector).ToString();
                Cod    = int.Parse(jsonData.SelectToken(CODSelector).ToString());
            }
            else
            {
                ValidRequest = false;
            }
        }
Exemplo n.º 4
0
        public Query(string apiKey, string queryStr)
        {
            JObject jsonData;

            using (var client = new System.Net.WebClient())
                jsonData = JObject.Parse(client.DownloadString($"http://api.openweathermap.org/data/2.5/weather?appid={apiKey}&q={queryStr}"));

            if (jsonData.SelectToken("cod").ToString() == "200")
            {
                ValidRequest = true;
                Coord        = new Coord(jsonData.SelectToken("coord"));
                foreach (JToken weather in jsonData.SelectToken("weather"))
                {
                    Weathers.Add(new Weather(weather));
                }
                Base = jsonData.SelectToken("base").ToString();
                Main = new Main(jsonData.SelectToken("main"));
                if (jsonData.SelectToken("visibility") != null)
                {
                    Visibility = double.Parse(jsonData.SelectToken("visibility").ToString(), CultureInfo.InvariantCulture);
                }
                Wind = new Wind(jsonData.SelectToken("wind"));
                if (jsonData.SelectToken("rain") != null)
                {
                    Rain = new Rain(jsonData.SelectToken("rain"));
                }
                if (jsonData.SelectToken("snow") != null)
                {
                    Snow = new Snow(jsonData.SelectToken("snow"));
                }
                Clouds = new Clouds(jsonData.SelectToken("clouds"));
                Sys    = new Sys(jsonData.SelectToken("sys"));
                ID     = int.Parse(jsonData.SelectToken("id").ToString(), CultureInfo.InvariantCulture);
                Name   = jsonData.SelectToken("name").ToString();
                Cod    = int.Parse(jsonData.SelectToken("cod").ToString(), CultureInfo.InvariantCulture);
                Dt     = Helper.convertUnixToDateTime(double.Parse(jsonData.SelectToken("dt").ToString(), CultureInfo.InvariantCulture));
            }
            else
            {
                ValidRequest = false;
            }
        }
Exemplo n.º 5
0
        public QueryResponse(string jsonResponse)
        {
            var jsonData = JObject.Parse(jsonResponse);

            if (jsonData.SelectToken("cod").ToString() == "200")
            {
                ValidRequest = true;
                Coordinates  = new Coordinates(jsonData.SelectToken("coord"));
                foreach (JToken weather in jsonData.SelectToken("weather"))
                {
                    WeatherList.Add(new Weather(weather));
                }
                Base = jsonData.SelectToken("base").ToString();
                Main = new Main(jsonData.SelectToken("main"));
                if (jsonData.SelectToken("visibility") != null)
                {
                    Visibility = double.Parse(jsonData.SelectToken("visibility").ToString(), CultureInfo.InvariantCulture);
                }
                Wind = new Wind(jsonData.SelectToken("wind"));
                if (jsonData.SelectToken("rain") != null)
                {
                    Rain = new Rain(jsonData.SelectToken("rain"));
                }
                if (jsonData.SelectToken("snow") != null)
                {
                    Snow = new Snow(jsonData.SelectToken("snow"));
                }
                Clouds = new Clouds(jsonData.SelectToken("clouds"));
                Sys    = new Sys(jsonData.SelectToken("sys"));
                ID     = int.Parse(jsonData.SelectToken("id").ToString(), CultureInfo.InvariantCulture);
                Name   = jsonData.SelectToken("name").ToString();
                Cod    = int.Parse(jsonData.SelectToken("cod").ToString(), CultureInfo.InvariantCulture);
            }
            else
            {
                ValidRequest = false;
            }
        }
Exemplo n.º 6
0
        public Query(string apiKey, double vLat, double vLong)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            JObject jsonData        = JObject.Parse(wc.DownloadString(string.Format("http://api.openweathermap.org/data/2.5/weather?appid={0}&lat={1}&lon={2}", apiKey, vLat.ToString(), vLong.ToString())));

            //JObject jsonData = JObject.Parse(new System.Net.WebClient().DownloadString(string.Format("http://api.openweathermap.org/data/2.5/weather?appid={0}&lat={1}&lon={2}", apiKey, vLat.ToString(), vLong.ToString())));

            // JObject jsonData = JObject.Parse(new System.Net.WebClient().DownloadString(string.Format("http://api.openweathermap.org/data/2.5/weather?appid={0}&q={1}", apiKey, queryStr)));
            if (jsonData.SelectToken("cod").ToString() == "200")
            {
                validRequest = true;
                if (jsonData.SelectToken("coord") != null)
                {
                    coord = new Coord(jsonData.SelectToken("coord"));
                }
                foreach (JToken weather in jsonData.SelectToken("weather"))
                {
                    weathers.Add(new Weather(weather));
                }
                baseStr = jsonData.SelectToken("base").ToString();
                if (jsonData.SelectToken("main") != null)
                {
                    main = new Main(jsonData.SelectToken("main"));
                }
                if (jsonData.SelectToken("visibility") != null)
                {
                    visibility = double.Parse(jsonData.SelectToken("visibility").ToString());
                }
                wind = new Wind(jsonData.SelectToken("wind"));
                if (jsonData.SelectToken("raid") != null)
                {
                    rain = new Rain(jsonData.SelectToken("rain"));
                }
                if (jsonData.SelectToken("snow") != null)
                {
                    snow = new Snow(jsonData.SelectToken("snow"));
                }
                clouds = new Clouds(jsonData.SelectToken("clouds"));
                if (jsonData.SelectToken("sys") != null)
                {
                    sys = new Sys(jsonData.SelectToken("sys"));
                }
                if (jsonData.SelectToken("id") != null)
                {
                    id = int.Parse(jsonData.SelectToken("id").ToString());
                }
                if (jsonData.SelectToken("name") != null)
                {
                    name = jsonData.SelectToken("name").ToString();
                }
                if (jsonData.SelectToken("cod") != null)
                {
                    cod = int.Parse(jsonData.SelectToken("cod").ToString());
                }
            }
            else
            {
                validRequest = false;
            }

            wc.Dispose();
        }