コード例 #1
0
        public override async Task <ObservableCollection <LocationQueryViewModel> > GetLocations(string location_query)
        {
            ObservableCollection <LocationQueryViewModel> locations = null;

            string yahooAPI = "https://query.yahooapis.com/v1/public/yql?q=";
            string query    = "select * from geo.places where text=\"" + location_query + "*\"";
            Uri    queryURL = new Uri(yahooAPI + query);
            // Limit amount of results shown
            int maxResults = 10;

            try
            {
                // Connect to webstream
                HttpClient          webClient = new HttpClient();
                HttpResponseMessage response  = await webClient.GetAsync(queryURL);

                response.EnsureSuccessStatusCode();
                Stream contentStream = null;
#if WINDOWS_UWP
                contentStream = WindowsRuntimeStreamExtensions.AsStreamForRead(await response.Content.ReadAsInputStreamAsync());
#elif __ANDROID__
                contentStream = await response.Content.ReadAsStreamAsync();
#endif
                // End Stream
                webClient.Dispose();

                // Load data
                locations = new ObservableCollection <LocationQueryViewModel>();
                XmlSerializer deserializer = new XmlSerializer(typeof(query));
                query         root         = (query)deserializer.Deserialize(contentStream);

                foreach (place result in root.results)
                {
                    // Filter: only store city results
                    if (result.placeTypeName.Value == "Town" ||
                        result.placeTypeName.Value == "Suburb" ||
                        (result.placeTypeName.Value == "Zip Code" ||
                         result.placeTypeName.Value == "Postal Code" &&
                         (result.locality1 != null && result.locality1.type == "Town") ||
                         (result.locality1 != null && result.locality1.type == "Suburb")))
                    {
                        locations.Add(new LocationQueryViewModel(result));
                    }
                    else
                    {
                        continue;
                    }

                    // Limit amount of results
                    maxResults--;
                    if (maxResults <= 0)
                    {
                        break;
                    }
                }

                // End Stream
                if (contentStream != null)
                {
                    contentStream.Dispose();
                }
            }
            catch (Exception ex)
            {
                locations = new ObservableCollection <LocationQueryViewModel>();
                Logger.WriteLine(LoggerLevel.Error, ex, "HEREWeatherProvider: error getting locations");
            }

            if (locations == null || locations.Count == 0)
            {
                locations = new ObservableCollection <LocationQueryViewModel>()
                {
                    new LocationQueryViewModel()
                }
            }
            ;

            return(locations);
        }
コード例 #2
0
        public override async Task <LocationQueryViewModel> GetLocation(string location_query)
        {
            LocationQueryViewModel location = null;

            string           yahooAPI = "https://query.yahooapis.com/v1/public/yql?q=";
            string           query    = "select * from geo.places where text=\"" + location_query + "\"";
            Uri              queryURL = new Uri(yahooAPI + query);
            place            result   = null;
            WeatherException wEx      = null;

            try
            {
                // Connect to webstream
                HttpClient          webClient = new HttpClient();
                HttpResponseMessage response  = await webClient.GetAsync(queryURL);

                response.EnsureSuccessStatusCode();
                Stream contentStream = null;
#if WINDOWS_UWP
                contentStream = WindowsRuntimeStreamExtensions.AsStreamForRead(await response.Content.ReadAsInputStreamAsync());
#elif __ANDROID__
                contentStream = await response.Content.ReadAsStreamAsync();
#endif

                // End Stream
                webClient.Dispose();

                // Load data
                XmlSerializer deserializer = new XmlSerializer(typeof(query));
                query         root         = (query)deserializer.Deserialize(contentStream);

                if (root.results != null)
                {
                    result = root.results[0];
                }

                // End Stream
                if (contentStream != null)
                {
                    contentStream.Dispose();
                }
            }
            catch (Exception ex)
            {
                result = null;
#if WINDOWS_UWP
                if (Windows.Web.WebError.GetStatus(ex.HResult) > Windows.Web.WebErrorStatus.Unknown)
                {
                    wEx = new WeatherException(WeatherUtils.ErrorStatus.NetworkError);
                    await Toast.ShowToastAsync(wEx.Message, ToastDuration.Short);
                }
#elif __ANDROID__
                if (ex is System.Net.WebException || ex is HttpRequestException)
                {
                    wEx = new WeatherException(WeatherUtils.ErrorStatus.NetworkError);
                    new Android.OS.Handler(Android.OS.Looper.MainLooper).Post(() =>
                    {
                        Toast.MakeText(Application.Context, wEx.Message, ToastLength.Short).Show();
                    });
                }
#endif
                Logger.WriteLine(LoggerLevel.Error, ex, "HEREWeatherProvider: error getting location");
            }

            if (result != null && !String.IsNullOrWhiteSpace(result.woeid))
            {
                location = new LocationQueryViewModel(result);
            }
            else
            {
                location = new LocationQueryViewModel();
            }

            return(location);
        }