Search code examples
iosjsonoffset

Get the last 10 articles via JSON from newspaper API works but how to load the next 10?


Working on an iPhone App fetches newspaper articles from the web.

For performance purposes only the last 10 articles will be send per request.

All works as expected but how do I get the next 10 articles when user scrolls down and wants to read more?

  • Working: First request gives me articles: 1 - 10
  • NOT Working: Request send again should give me articles: 11 - 20 (by setting an offset)

(When I read the API documentation I am supposed to set an offset to get the next articles but it doesn't work, I always get the same first 10 articles based on my search query.)

Following request searches for the last 10 articles with the keyword frankfurt in it and sets an offset of 10 which should send me the next 10 articles when I send the request again.

http://api.zeit.de/content?q=frankfurt&limit=10&offset=10&api_key=MY_API_KEY

From the API documentation:

Search results are limited to 10 matches by default. You can increase this value with the limit parameter. To iterate over the resultset, repeat your request with the offset parameter set to multiples of the limit.

UPDATE:

- (IBAction)runBtnTapped:(id)sender
{
    NSError *error;
    
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://api.zeit.de/content?q=frankfurt&fields=teaser_title%20AND%20release_date&limit=5&offset=5&sort=release_date%20desc&api_key=123456789"]];
    
    NSDictionary *dictFromData = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    
    NSArray *array = dictFromData[@"matches"];
    NSLog(@"%@", array);
}

CONSOLE OUTPUT:

2013-01-10 17:21:39.261 ZEITreisen[94644:c07] (
        {
        "release_date" = "2013-01-03T06:00:00Z";
        "teaser_title" = "Im Stich gelassen";
    },
        {
        "release_date" = "2012-12-28T13:18:58Z";
        "teaser_title" = "Serbiens Nachwuchs baut den Weg nach Europa";
    },
        {
        "release_date" = "2012-12-27T10:18:07Z";
        "teaser_title" = "Das Fu\U00dfballgott bestrafte Hoffenheim";
    },
        {
        "release_date" = "2012-12-27T06:00:00Z";
        "teaser_title" = "Sind Fitschen und Jain die Richtigen f\U00fcr 2013?";
    },
        {
        "release_date" = "2012-12-27T06:00:00Z";
        "teaser_title" = Sparen;
    }
)
2013-01-10 17:21:43.447 ZEITreisen[94644:c07] (
        {
        "release_date" = "2013-01-03T06:00:00Z";
        "teaser_title" = "Im Stich gelassen";
    },
        {
        "release_date" = "2012-12-28T13:18:58Z";
        "teaser_title" = "Serbiens Nachwuchs baut den Weg nach Europa";
    },
        {
        "release_date" = "2012-12-27T10:18:07Z";
        "teaser_title" = "Das Fu\U00dfballgott bestrafte Hoffenheim";
    },
        {
        "release_date" = "2012-12-27T06:00:00Z";
        "teaser_title" = "Sind Fitschen und Jain die Richtigen f\U00fcr 2013?";
    },
        {
        "release_date" = "2012-12-27T06:00:00Z";
        "teaser_title" = Sparen;
    }
)

Solution

  • You might running into an instance where the API incorrectly prioritizes some parameters over others to the point of ignoring your offset. Try removing your limit since you're using the default, or try using different numbers between limit and offset. There may be some poor comparisons being made that are preventing your offset from being accepted. Without API docs, it is hard to say though what could be going wrong. I would second @Geraud.ch's suggestion though if modifying your query string doesn't work: contact the API provider.

    Edit just realized you posted the actual URL. I'm searching for docs now.