【问题标题】:Twitter Streaming Endpoint not working with SLRequest objectTwitter Streaming Endpoint 不适用于 SLRequest 对象
【发布时间】:2014-01-28 22:23:39
【问题描述】:

我正在尝试使用 SLRequest 类从 Twitter 流 API 中提取数据。 当我使用程序“挂起”下面的代码中记录的端点和参数时 并且不打印任何 JSON 数据。我正在使用基于 twitter dev 示例的端点 网站https://dev.twitter.com/docs/streaming-apis/parameters#with 我要求 在特定位置发推文。

当我使用此代码通过 REST API 查询我的时间线时(包括代码和请求,但 注释掉)程序没有挂起,我得到一个有效的响应。

我需要在代码中实现使用流式 API 访问数据的其他内容吗?需要进行哪些额外的修改或更改?

ACAccountStore * accountStore = [[ACAccountStore alloc] init];
ACAccountType * twitterAccountType =
[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

// Ask the user permission to access his account
[accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
    if (granted == NO) {
        NSLog(@"-- error: %@", [error localizedDescription]);
    }
    if (granted == YES){

       /***************** Create  request using REST API*********************
        ***************** This URL is functional and returns valid data *****
        NSURL * url = [NSURL URLWithString:@"https://userstream.twitter.com/1.1/user.json"];
         SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:@{@"screen_name": @"your_twitter_id"}];
        ***************************************************************/

        // Create request using Streaming API Endpoint
        NSURL * url = [NSURL URLWithString:@"https://stream.twitter.com/1.1/statuses/filter.json"];

        NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
        [params setObject:@"track" forKey:@"twitter&"];
        [params setObject:@"locations" forKey:@"-122.75,36.8,-121.75,37.8"];

        SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params];

        NSArray * twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];

        if ([twitterAccounts count] == 0) {
            (NSLog(@"-- no accounts available"));
        } else if ([twitterAccounts count] >0){
            [request setAccount:[twitterAccounts lastObject]];
            NSLog([request.account description]);
            NSLog(@"Twitter handler of user is %@", request.account.username);

            // Execute the request
            [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                NSError * jsonError = nil;
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];

                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    // NSLog(@"-- json Data is %@", json);
                    NSLog([json description]);
                }];

            }];

        }
    }

}];

【问题讨论】:

    标签: api twitter streaming endpoint slrequest


    【解决方案1】:

    SLRequest 不能很好地与流式 API 配合使用。

    这里是STTwitter的处理方法:

    self.twitter = [STTwitterAPI twitterAPIOSWithAccount:account];
    
    [_twitter verifyCredentialsWithSuccessBlock:^(NSString *username) {
    
        NSLog(@"-- access granted for %@", username);
    
        [_twitter postStatusesFilterUserIDs:nil
                            keywordsToTrack:@[@"twitter"]
                      locationBoundingBoxes:@[@"-122.75,36.8,-121.75,37.8"]
                                  delimited:nil
                              stallWarnings:nil
                              progressBlock:^(id response) {
            NSLog(@"-- %@", response);
        } stallWarningBlock:^(NSString *code, NSString *message, NSUInteger percentFull) {
            NSLog(@"-- stall warning");
        } errorBlock:^(NSError *error) {
            NSLog(@"-- %@", [error localizedDescription]);
        }];
    
    } errorBlock:^(NSError *error) {
        NSLog(@"-- %@", [error localizedDescription]);
    }];
    

    在内部,STTwitter 使用来自-[SLRequest preparedURLRequest] 的请求构建一个NSURLConnection 实例。如果你愿意,你可以在你的代码中复制this trick

    【讨论】:

      猜你喜欢
      • 2013-06-22
      • 2012-10-04
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 2016-11-10
      • 2012-06-10
      • 2021-04-28
      • 2017-09-12
      相关资源
      最近更新 更多