示例#1
0
        async void getCurrentConditions()
        {
            //  string weatherDataResp;

            // Call OpenWeatherMap API.
            string response;

            using (HttpClient client = new HttpClient())
            {
                string url = "https://finalprojectcomp494.azurewebsites.net/api/weather/6";
                response = await client.GetStringAsync(url);
            }
            // Deserialize the data from JSON response.
            WeatherData = JsonConvert.DeserializeObject <OpenWeatherMapData>(response);

            // Format date, time and image path and copy the results to allWeatherData
            foreach (var observationPoint in WeatherData.list)
            {
                System.DateTime timestamp = System.DateTimeOffset.FromUnixTimeSeconds(observationPoint.dt).DateTime;
                observationPoint.datetime        = timestamp;
                observationPoint.weather[0].icon = $"/Assets/{observationPoint.weather[0].icon}.png";
                allWeatherData.Add(observationPoint);
            }
            //    ResultTextBlock.Text = data.tempF + " 'F";

            //  ResultTextBlock.Text = "Today weather " + data[0].tempF+ " F" ;
        }
        /// <summary>
        /// Asynchronous function to retrieve data from OpenWeatherMap API.
        /// </summary>
        /// <param name="city">Name of the city for which the weather data is retrieved.</param>
        /// <param name="allWeatherData">Observable collection for storing the weather data</param>
        /// <param name="apiKey">API key for OpenWeatherMap</param>
        /// <returns></returns>
        public static async Task GetWeatherDataAsync(
            string city,
            ObservableCollection <WeatherCheck.Models.List> allWeatherData,
            string apiKey)
        {
            // Check that a city name is provided.
            if (String.IsNullOrEmpty(city))
            {
                throw new Exception("Please, select a city first!");
            }

            // Handle responsedata
            string             weatherDataResp;
            OpenWeatherMapData WeatherData = new OpenWeatherMapData();

            // Call OpenWeatherMap API.
            using (var HttpClient = new HttpClient())
            {
                string apiCall = $"http://api.openweathermap.org/data/2.5/forecast?q={ city },{ country }&units={ units }&appid={ apiKey }";
                weatherDataResp = await HttpClient.GetStringAsync(apiCall);
            }

            // Deserialize the data from JSON response.
            WeatherData = JsonConvert.DeserializeObject <OpenWeatherMapData>(weatherDataResp);
            OpenWeatherMapData wt = new OpenWeatherMapData();

            // Format date, time and image path and copy the results to allWeatherData
            foreach (var observationPoint in WeatherData.list)
            {
                System.DateTime timestamp = System.DateTimeOffset.FromUnixTimeSeconds(observationPoint.dt).DateTime;
                observationPoint.datetime        = timestamp;
                observationPoint.weather[0].icon = $"/Resources/WeatherImages/{observationPoint.weather[0].icon}.png";
                allWeatherData.Add(observationPoint);
            }
        }
        public void Lookup()
        {
            var uriBuilder = new UriBuilder(_url);

            uriBuilder.Query = $"q={City}&units=metric&appid={_apiKey}";

            ResponseText = RestApiUtil.Get(uriBuilder.Uri);

            if (JsonUtil.IsValidJson(ResponseText))
            {
                ResponseData = JsonUtil.Deserialize <OpenWeatherMapData>(ResponseText);
            }
        }