Example #1
0
        private async void LoadQuotes()
        {
            var quotes = await _quotes.GetQuotes();

            foreach (var quote in quotes)
            {
                QuotesCollection.Add(quote);
            }
        }
Example #2
0
        /// <summary>
        /// This async method will fetch JSON (text) file stored in DropBox shared folder and parse it to QuotesModel.
        /// After parsing these quotes will be added to observable collection.
        /// </summary>
        /// <returns></returns>
        async Task GetQuotes()
        {
            if (IsBusy)
            {
                return;
            }

            Exception error = null;

            try
            {
                IsBusy = true;
                using (var client = new HttpClient())
                {
                    //grab json from server
                    var json = await client.GetStringAsync("https://www.dropbox.com/s/4o74hgxgkul0i05/Quotes.json?dl=1");

                    var items = JsonConvert.DeserializeObject <List <QuotesModel> >(json);
                    QuotesCollection.Clear();
                    foreach (var item in items)
                    {
                        QuotesCollection.Add(item);
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex;
                Debug.WriteLine("Error: " + ex);
                error = ex;
            }
            finally
            {
                IsBusy = false;
            }
            if (error != null)
            {
                await Application.Current.MainPage.DisplayAlert("Error!", error.Message, "OK");
            }
        }