2014-01-28 26 views
0

我正在尝试使用SLRequest类从Twitter Streaming API中提取数据。 当我使用下面的代码中记录的端点和参数“挂起” 并且没有打印JSON数据时。我正在使用基于来自twitter dev 网站https://dev.twitter.com/docs/streaming-apis/parameters#with的示例的端点。我正在请求 在特定位置的推文。Twitter Streaming端点不能与SLRequest对象一起使用

当我使用此代码使用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]); 
       }]; 

      }]; 

     } 
    } 

}]; 

回答

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建立一个NSURLConnection实例与-[SLRequest preparedURLRequest]请求。如果您愿意,您可以在代码中复制this trick

相关问题