Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NewsApi returning totalResults=20 even after specifying pageSize=100

I'm using News API in android app.

I'm trying to get more results (news) from the server but it is always returning just 20 results which has been set as default as mentioned in the docs here.

Here's my code:

class DownloadNews extends AsyncTask<String, Void, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
        protected String doInBackground(String... args) {
            String xml = "";

            String urlParameters = "";
            xml = Function.excuteGet("https://newsapi.org/v2/top-headlines?country=in&pageSize=100&apiKey=******************", urlParameters);
            return xml;
        }
        @Override
        protected void onPostExecute(final String xml) {

            if (xml != null) {
                if (xml.length() > 10) { // Just checking if not empty

                    Log.d("xml", xml);

                } else {

                }
            } else {

            }
        }

    }

I've specified pageSize argument as 100 but still I'm getting just 20 totalResults:

09-29 22:29:22.241 10275-10275/com.abc.xyz D/xml: {"status":"ok","totalResults":20,"articles":[]}

What's going wrong here?

like image 886
Hammad Nasir Avatar asked Sep 29 '18 17:09

Hammad Nasir


3 Answers

It's the API having a problem. It looks like there are only 20 news items available for 'India' (if 'in' stands for that) and that 20 appears to have been hard-coded or the total number of items is being returned regardless of the pageSize param.

In Postman, I tried with pageSize=100 like you did and still got "totalResults": 20. I again tried pageSize=10 and interestingly, totalResults remained 20 but got 10 articles back as specified.

like image 107
Dut A. Avatar answered Oct 06 '22 01:10

Dut A.


The /top-headlines endpoint only returns a max of 20 articles. When an article is no longer considered a 'top headline' it drops off this endpoint. If you need to retrieve more than 20 articles you can use the /everything endpoint, which includes all top headlines and any other smaller articles too.

like image 39
edgarian Avatar answered Oct 06 '22 00:10

edgarian


API's are working fine I have just implemented it, just consider the page as 1,2...so on i.e when you get the first 20records(or as per page size) set your page to 1 and then increment the page 2 for second request.

In swift I have implemented it below

    let currentPage = self.articles.count/pageSize
    fetchHeadlines(page: currentPage+1)

Hope it helps others!!!!

like image 21
navroz Avatar answered Oct 05 '22 23:10

navroz