【发布时间】: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