【问题标题】:iOS twitter API loadings more than 20 followers/following/tweetsiOS twitter API 加载超过 20 个关注者/关注者/推文
【发布时间】:2013-11-18 16:59:50
【问题描述】:

我有一个简单的 Twitter 客户端,用于显示用户的推文、关注者和关注者。

由于某种原因,用户推文的计数参数被忽略了,它总是只加载 20 个结果。

代码如下:

- (void)getUserTweets {

    // 1. Create a variable for twitter
    NSURL *feedURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];

    // 2. Get AppDelegate reference
    AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

    // 3. Create NSDictionary for the TWR parameters
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: self.usernameToLoad, @"screen_name", @"true", @"include_user_entities", @"100", @"count", nil];


    // 4. Create TWRequest, with parameters, URL & specify GET method
    //TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL parameters:parameters requestMethod:TWRequestMethodGET];
    SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:feedURL parameters:params];

    // 5. Specify twitter request's account to our app delegate's
    twitterFeed.account = appDelegate.userAccount;


    // Making the request

    [twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{

            // Check if we reached the reate limit

            if ([urlResponse statusCode] == 429) {
                NSLog(@"Rate limit reached");
                return;
            }

            // Check if there was an error

            if (error) {
                NSLog(@"Error: %@", error.localizedDescription);
                return;
            }

            // Check if there is some response data

            if (responseData) {

                NSError *error = nil;
                self.userTweets = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];

            }
        });
    }];

}

这将始终只返回 20 个结果,即使我将计数设置为 1 或 2 这样的低值。我在定义参数时是否做错了什么?

另外,我正在尝试加载用户的关注者和关注者,但我想加载总共 200 个,但同样只加载 20 个。

从 twitter API 读取的内容来看,它提供自动分页,并使用光标参数,我可以遍历以加载我想要的所有数据。

我很难弄清楚这是如何工作的。这是我的跟随代码(跟随者是相同的,只是它调用不同的 API 字符串)

- (void)getFriends {

    // 1. Create a variable for twitter
    NSURL *feedURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/friends/list.json"];

    // 2. Get AppDelegate reference
    AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

    // 3. Create NSDictionary for the TWR parameters
    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys: self.usernameToLoad, @"screen_name", @"true", @"include_user_entities", nil];

    // 4. Create TWRequest, with parameters, URL & specify GET method
    //TWRequest *twitterFeed = [[TWRequest alloc] initWithURL:feedURL parameters:parameters requestMethod:TWRequestMethodGET];
    SLRequest *twitterFeed = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:feedURL parameters:parameters];

    // 5. Specify twitter request's account to our app delegate's
    twitterFeed.account = appDelegate.userAccount;


    // Making the request

    [twitterFeed performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        dispatch_async(dispatch_get_main_queue(), ^{

            // Check if we reached the reate limit

            if ([urlResponse statusCode] == 429) {
                NSLog(@"Rate limit reached");
                return;
            }

            // Check if there was an error

            if (error) {
                NSLog(@"Error: %@", error.localizedDescription);
                return;
            }

            // Check if there is some response data

            if (responseData) {

                NSError *error = nil;
                NSDictionary *TWData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];



                self.userFriends = [TWData objectForKey:@"users"];

            }
        });
    }];

}

我不确定如何正确循环,因为 twitter api 返回我需要转到下一个数据的光标值。

任何帮助都会很棒,我可能只是错过了一些我无法完全理解的逻辑。

提前致谢!

【问题讨论】:

    标签: ios api twitter


    【解决方案1】:

    结果在多个“页面”中给出,可以在后续请求中使用 next_cursor 值进行导航。

    https://dev.twitter.com/docs/misc/cursoring

    【讨论】:

      【解决方案2】:

      很好,我在分页推文提要时也遇到了同样的问题,它从未用于更新“max_id”参数,因为它的值是“NSNumber”,然后我改为 NSString 然后它对我来说非常好用,Cross再次检查请求中是否使用了 NSString 以外的任何对象

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-07-20
        • 2013-07-01
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 2011-10-13
        • 2010-12-23
        • 2014-01-06
        相关资源
        最近更新 更多