2013-07-27 78 views
0

我試圖使一個應用程序連接到OAUth 1的Web服務,我遇到了一些麻煩。這裏是我的方法和我的GET請求:AFNetworking獲取請求,獲取NSURLErrorDomain代碼= -1005

- (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,之前我從來沒有見過這個錯誤,所以我不確定如何解決它。有任何想法嗎?

回答

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]; 
0

這裏的問題是AFHTTPClient在您的方法-loadUserProfile的末尾內存不足。

您需要重構此代碼,以便客戶端在請求後仍保留在內存中。

做到這一點的一種方法是使用AFNetworking中示例代碼中所示的單例模式。

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

+0

你有把握嗎?我試圖讓一個'我的HttpClient變量的strong'財產,我也得到了同樣的錯誤。強大的性能不應該得到釋放,對不對? – Anders

+0

試圖使singelton的建議,有點無法得到它的工作。 – Anders

0

在我的情況下,解決方案是重新啓動Xcode和模擬器。

該URL確實可以在桌面瀏覽器中使用,並且可以使用curl。認識到這一點給一點暗示的東西可能是錯誤的環境。

FWIW,Xcode的版本是6.0測試版,打造6A279r。