예제 #1
0
        private async void GetGalNetNewsAsync(CancellationTokenSource cancelToken, bool ignoreCache = false)
        {
            using (UserDialogs.Instance.Loading("Loading", () => cancelToken.Cancel(), null, true, MaskType.Clear))
            {
                try
                {
                    // get the news feed
                    string        json = String.Empty;
                    GalNetService news = new GalNetService();
                    (json, LastUpdated) = await news.GetData(cancelToken, ignoreCache).ConfigureAwait(false);

                    // parse the json data
                    GalNetNewsList.Clear();
                    await Task.Run(() =>
                    {
                        List <NewsItem> fullNews = JsonConvert.DeserializeObject <List <NewsItem> >(json, NewsItemConverter.Instance);
                        foreach (NewsItem item in fullNews.Where(o => !String.IsNullOrEmpty(o.Body)).OrderByDescending(o => o.PublishDateTime).Take(15))
                        {
                            item.ClassifyArticle();
                            Device.BeginInvokeOnMainThread(() => GalNetNewsList.Add(item));
                        }
                    }).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    SetMessage("GalNet News download was cancelled or timed out.", true);
                }
                catch (HttpRequestException ex)
                {
                    string err   = ex.Message;
                    int    start = err.IndexOf("OPENSSL_internal:", StringComparison.OrdinalIgnoreCase);
                    if (start > 0)
                    {
                        start += 17;
                        int end = err.IndexOf(" ", start, StringComparison.OrdinalIgnoreCase);
                        err = String.Format("SSL Error ({0})", err.Substring(start, end - start).Trim());
                    }
                    else if (err.IndexOf("Error:", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        err = err.Substring(err.IndexOf("Error:", StringComparison.OrdinalIgnoreCase) + 6).Trim();
                    }
                    SetMessage(String.Format("Network Error: {0}", err), true);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("unexpected end of stream"))
                    {
                        SetMessage("GalNet News download was cancelled.", true);
                    }
                    else
                    {
                        SetMessage(String.Format("Error: {0}", ex.Message), true);
                    }
                }
            }
        }
예제 #2
0
        private async void GetGalNetNewsAsync(CancellationTokenSource cancelToken, bool ignoreCache = false)
        {
            using (UserDialogs.Instance.Loading("Loading", () => cancelToken.Cancel(), null, true, MaskType.Clear))
            {
                try
                {
                    List <NewsArticle> newsList = new List <NewsArticle>();
                    GalNetService      news     = GalNetService.Instance();
                    (newsList, LastUpdated) = await news.GetData(12, settings.NewsCacheTime, cancelToken, ignoreCache : ignoreCache).ConfigureAwait(false);

                    GalNetNewsList.Clear();
                    if (newsList?.Any() == false)
                    {
                        SetMessage("No GalNet articles found.", false);
                    }
                    else
                    {
                        foreach (NewsArticle item in newsList)
                        {
                            GalNetNewsList.Add(item);
                        }
                    }
                }
                catch (OperationCanceledException)
                {
                    SetMessage("GalNet News download was cancelled or timed out.", true);
                }
                catch (HttpRequestException ex)
                {
                    string errorMessage = ex.Message;
                    if (errorMessage.IndexOf("OPENSSL_internal:", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        errorMessage = "A secure connection could not be established.";
                    }
                    else if (errorMessage.IndexOf("Error:", StringComparison.OrdinalIgnoreCase) > 0)
                    {
                        errorMessage = errorMessage.Substring(errorMessage.IndexOf("Error:", StringComparison.OrdinalIgnoreCase) + 6).Trim();
                    }
                    SetMessage($"Network Error: {errorMessage}", true);
                }
                catch (Exception ex)
                {
                    if (ex.Message.Contains("unexpected end of stream"))
                    {
                        SetMessage("GalNet News download was cancelled.", true);
                    }
                    else
                    {
                        SetMessage($"Error: {ex.Message}", true);
                    }
                }
            }
        }