private async void LoadNewsList()
        {
            try
            {
                IsBusy = true;

                if (_connectivity.NetworkAccess != NetworkAccess.Internet)
                {
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        _dialogService.DisplayAlert(AppResources.ConnectionPleaseCheckInternet);
                    });

                    return;
                }

                TopHeadlines latestNews = await _newsService.GetTopNewsByCategory("us", newsCategory, "f7cb4296dd7b4311b6c5e099345c71ab");

                TechnologyNews = new ObservableCollection <Article>(latestNews.articles);
            }
            catch (Exception ex)
            {
                _dialogService.DisplayError(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
        public async Task <IActionResult> getTopNewsByCategory(String category = null, String country = null, String sources = null, String q = null, int?pageSize = null, int?page = null)
        {
            using (var httpClient = new HttpClient()) {
                try
                {
                    String       url          = "https://newsapi.org/v2/top-headlines?";
                    TopHeadlines topHeadlines = null;
                    if (category != null)
                    {
                        url = url + "category=" + category + "&";
                    }
                    if (country != null)
                    {
                        url = url + "country=" + country + "&";
                    }
                    if (sources != null)
                    {
                        url = url + "sources=" + sources + "&";
                    }
                    if (q != null)
                    {
                        url = url + "q=" + q + "&";
                    }
                    if (pageSize != null)
                    {
                        url = url + "pageSize=" + pageSize + "&";
                    }
                    if (page != null)
                    {
                        url = url + "page=" + page + "&";
                    }
                    url = url + "apiKey=" + API_KEY;
                    //GET Method
                    //  httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", API_KEY);
                    var response = await httpClient.GetAsync(new Uri(url)).ConfigureAwait(false);

                    response.EnsureSuccessStatusCode();
                    var stringResult = await response.Content.ReadAsStringAsync();

                    var rawData = JsonConvert.DeserializeObject <TopHeadlines>(stringResult);
                    topHeadlines = new TopHeadlines {
                        Status       = rawData.Status,
                        TotalResults = rawData.TotalResults,
                        Articles     = rawData.Articles
                    };

                    return(Ok(topHeadlines));
                }
                catch (HttpRequestException httpRequestException)
                {
                    return(BadRequest($"Error getting data: {httpRequestException.Message}"));
                }
            }
        }
Beispiel #3
0
        protected override async void Invoke(ContentPage contentPage)
        {
            switch (NetworkService.ExistsInternetConnection())
            {
            case true:
                //await contentPage.DisplayAlert("Internet Connection", "You are connected", "OK");
                restServices = new RestServices();
                topHeadlines = await restServices.GetTopHeadlines();

                break;

            case false:
                await contentPage.DisplayAlert("Internet Connection", "You are NOT connected. Please, get a connection", "OK");

                break;
            }
        }
        public async Task <List <NewsArticle> > BindAllNewsHeadlines()
        {
            List <NewsArticle> newsArticles      = new List <NewsArticle>();
            GNewsAPI           gNewsAPIHeadlines = await GetGNewsLocalNGHeadlines();

            TopHeadlines newsAPIHeadlines = await GetTopHeadlines();

            if (gNewsAPIHeadlines != null)
            {
                foreach (GNewsArticle headline in gNewsAPIHeadlines.Articles)
                {
                    newsArticles.Add(
                        new NewsArticle
                    {
                        Source      = headline.Source.Name,
                        Title       = headline.Title,
                        PublishedAt = headline.PublishedAt,
                        Url         = headline.Url,
                        UrlToImage  = headline.Image
                    }
                        );
                }
            }
            if (newsAPIHeadlines != null)
            {
                foreach (Article topheadline in newsAPIHeadlines.Articles)
                {
                    newsArticles.Add(
                        new NewsArticle
                    {
                        Author      = topheadline.Author,
                        Source      = topheadline.Source.Name,
                        Title       = topheadline.Title,
                        PublishedAt = topheadline.PublishedAt.ToShortDateString(),
                        Url         = topheadline.Url,
                        UrlToImage  = topheadline.UrlToImage
                    }
                        );
                }
            }

            return(newsArticles);
        }