我试图使用SLRequest类从中提取数据。当我使用程序下面的代码中记录的端点和参数时,程序“挂起”,没有打印JSON数据。我使用的端点是基于来自twitter开发网站https://dev.twitter.com/docs/streaming-apis/parameters#with的一个例子,我在某个位置请求tweet。
当我使用这段代码来使用REST查询我的时间线时(包含了代码和请求,但是注释掉了),程序不会挂起,我会得到一个有效的响应。
在使用流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]);
}];
}];
}
}
}];发布于 2014-01-29 14:34:42
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实例。如果您愿意,可以在代码中复制这个诡计。
https://stackoverflow.com/questions/21418193
复制相似问题