【问题标题】:AFNetworking get request, getting NSURLErrorDomain Code=-1005AFNetworking 获取请求,获取 NSURLErrorDomain Code=-1005
【发布时间】:2013-07-27 08:29:22
【问题描述】:

我正在尝试制作一个使用 OAUth 1 连接到 Web 服务的应用,但遇到了一些问题。这是我的获取请求的方法:

- (void)loadUserProfile
{
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[baseUrl]];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                        path:[NSString  stringWithFormat:@"/1/user/%@/profile.json", [SSKeychain passwordForService:@"[service]" account:@"encoded_user_id"]]
                                                  parameters:nil];

    NSString *postString = [NSString  stringWithFormat:@"oauth_consumer_key=%@&oauth_token=%@&oauth_nonce=%@&oauth_signature_method=%@&oauth_timestamp=%@&oauth_version=%@", self.auth.consumerKey, [SSKeychain  passwordForService:@"[service]" account:@"oauth_token"], self.auth.nonce,  self.auth.signatureMethod, self.auth.timestamp, self.auth.version];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Print the response body in text
        NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    [operation start];
}

当我调用此方法时,出现以下错误:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."  UserInfo=0x8efa7f0  {NSErrorFailingURLStringKey=https://api.fitbit.com/1/user/[id]/profile.json,  NSErrorFailingURLKey=https://api.fitbit.com/1/user/[id]/profile.json,  NSLocalizedDescription=The network connection was lost., NSUnderlyingError=0x8bdb2c0 "The  network connection was lost."}

我想要获取的资源记录在这里:https://wiki.fitbit.com/display/API/OAuth+Authentication+in+the+Fitbit+API#OAuthAuthenticationintheFitbitAPI-tokenCredentials

我之前没用过OAuth 1,之前没见过这个错误,所以不知道怎么解决。有什么想法吗?

【问题讨论】:

    标签: objective-c cocoa-touch oauth afnetworking fitbit


    【解决方案1】:

    我使用Simple Oauth1 解决了这个问题。像这样:

    NSString *path = [NSString stringWithFormat:@"/1/user/%@/profile.json", [SSKeychain    passwordForService:@"Fitbit" account:@"fitbit_encoded_user_id"]];
    NSURLRequest *preparedRequest = [OAuth1Controller preparedRequestForPath:path
                                                                  parameters:nil
                                                                  HTTPmethod:@"GET"
                                                                  oauthToken:[SSKeychain passwordForService:@"Fitbit" account:@"fitbit_oauth_token"]
                                                                 oauthSecret:[SSKeychain passwordForService:@"Fitbit" account:@"fitbit_oauth_token_secret"]];
    
    AFJSONRequestOperation *operation = [AFJSONRequestOperation   JSONRequestOperationWithRequest:preparedRequest success:^(NSURLRequest *request,   NSHTTPURLResponse *response, id jsonObject) {
        NSLog(@"JSON: %@", jsonObject);
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
    [operation start];
    

    【讨论】:

      【解决方案2】:

      这里的问题是 AFHTTPClient 在你的方法结束时内存不足-loadUserProfile

      您需要重构此代码,以便客户端在您请求后仍保留在内存中。

      一种方法是使用单例模式,如 AFNetworking 中的示例代码所示。

      https://github.com/AFNetworking/AFNetworking/blob/master/Example/Classes/AFAppDotNetAPIClient.m#L31

      【讨论】:

      • 你确定吗?我试图为我的 httpClient 变量创建一个strong 属性,但我得到了同样的错误。强属性不应该被释放,对吧?
      • 尝试按照建议制作一个 singelton,有点无法让它工作。
      【解决方案3】:

      就我而言,解决方案是重启 Xcode 和模拟器。

      该 URL 在桌面浏览器和curl 中确实有效。意识到这一点会稍微暗示环境可能有问题。

      FWIW,Xcode 版本是 6.0 beta,build 6A279r。

      【讨论】:

        猜你喜欢
        • 2017-03-08
        • 2016-02-22
        • 2015-07-27
        • 2015-12-06
        • 2017-11-19
        • 2019-10-12
        • 2023-03-10
        • 2016-05-14
        • 1970-01-01
        相关资源
        最近更新 更多