首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用SLRequest对象

不使用SLRequest对象
EN

Stack Overflow用户
提问于 2014-01-28 22:23:39
回答 1查看 557关注 0票数 0

我试图使用SLRequest类从中提取数据。当我使用程序下面的代码中记录的端点和参数时,程序“挂起”,没有打印JSON数据。我使用的端点是基于来自twitter开发网站https://dev.twitter.com/docs/streaming-apis/parameters#with的一个例子,我在某个位置请求tweet。

当我使用这段代码来使用REST查询我的时间线时(包含了代码和请求,但是注释掉了),程序不会挂起,我会得到一个有效的响应。

在使用流API访问数据所需实现的代码中,是否还有其他东西需要实现?需要做哪些额外的修改或改变?

代码语言:javascript
复制
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]);
                }];

            }];

        }
    }

}];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-01-29 14:34:42

SLRequest没有很好地使用流API。

下面是如何使用STTwitter

代码语言:javascript
复制
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实例。如果您愿意,可以在代码中复制这个诡计

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21418193

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档