我正在尝试使用SLRequest类从Twitter Streaming 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]);
}];
}];
}
}
}];
答案 0 :(得分: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。