2013-02-06 106 views
1

我剛剛開始使用RestKit,並且剛到Rk 0.20即將開始生效,並且文檔和演示版本落後了一步。網上的大多數東西都是RK 0.10,0.20版本有很大變化。Restkit 0.20基本操作

我不想回到早期版本,而新版本很快就會啓動並運行。

我有一個URL 「test.myserver.com」,它返回一個簡單的數據報JSON資源 - { 「id_user」: 「4401」, 「datalocation」: 「4401」, 「國」: 「大英」, 「數據」: 「TESTDATA」, 「登錄」: 「弗雷德布羅格斯」, 「密碼」: 「579c0cb0ed2dc25db121283f7a98cc71」, 「ACCESSLEVEL」: 「2」, 「時間戳」:「1012 「, 」datahash「:」2749da29f20ce7a85092323f193adee8「 }

我很確定我有映射等排序,但我的服務r要求身份驗證,所以我需要將請求中的用戶名和密碼傳遞給服務器。

我有這個迄今爲止

NSURL *url = [NSURL URLWithString:@"http://test.myserver.com"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]]; 

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
RKLogInfo(@"Load collection of Articles: %@", mappingResult.array); 
} failure:^(RKObjectRequestOperation *operation, NSError *error) { 
RKLogError(@"Operation failed with error: %@", error); 
}]; 

     [objectRequestOperation start]; 

這似乎與服務器聯繫,但不可避免地記錄以下錯誤

restkit.network:RKObjectRequestOperation.m:296對象請求失敗:潛在的HTTP請求操作失敗,錯誤:Error Domain = org.restkit.RestKit.ErrorDomain Code = -1011「(200-299)中的預期狀態碼,得到401」UserInfo = 0x7884030 {NSLocalizedRecoverySuggestion = { 「error」:{ 「code」 401, 「消息」:「未經授權:身份驗證quired」 } },AFNetworkingOperationFailingURLRequestErrorKey = HTTP:在//elancovision.umfundi.com>,NSErrorFailingURLKey = http://elancovision.umfundi.com,NSLocalizedDescription =預期狀態碼(200-299),得到401,AFNetworkingOperationFailingURLResponseErrorKey =}

該課程的問題是我如何將用戶名和密碼添加到請求中。

對不起noob問題!

+0

您需要知道哪個協議用於API驗證。你能否在你的問題中確定它? RESTkit依靠AFNetworking爲其網絡層。因此,就認證而言,您必須處理此庫。 –

+0

最好我可以告訴你的是,服務器端是使用restlers庫在PHP中編寫的。 – nimbusgb

+0

使用'BasicAuthenticaton' – nimbusgb

回答

6

對於基本的HTTP身份驗證,應將每個請求的用戶名和密碼插入到HTTP請求授權標頭字段中。

首先,我建議你使用RKObjectManager集中配置請求和映射。 http://restkit.org/api/latest/Classes/RKObjectManager.html RKObjectManager可以存儲網絡參數(通過AFNetworking庫),然後根據用戶名/密碼,路徑,objectmapping建立適當的http查詢。

適應你的榜樣,它會看到這樣的:

NSURL* url = [[NSURL alloc]initWithString:@"http://test.myserver.com"]; 
RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:url]; 
[objectManager.HTTPClient setAuthorizationHeaderWithUsername:@"username" password:@"password"]; 

//NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
NSURLRequest *request = [objectManager requestWithObject:nil method:RKRequestMethodGET path:@"/yourAPI/yourmethod" parameters:nil]; 

RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]]; 

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
     RKLogInfo(@"Load collection of Articles: %@", mappingResult.array); 
    } failure:^(RKObjectRequestOperation *operation, NSError *error) { 
     RKLogError(@"Operation failed with error: %@", error); 
    }]; 

[objectRequestOperation start]; 

如果認證工作,有看RESTKit維基應該給你的下一個提示,以建立正確的映射:

+0

謝謝。就像你發佈的那樣,我通過暴力手段獲得了幾乎完全相同的答案!正如你所說維基給出了提示,它仍然集中在版本0.10,並且對版本0.2進行的更改非常重要。正確的.....對我的下一個挑戰,如何使用我現在可以從服務器獲得的信息! :) – nimbusgb

+0

+1 [objectManager.HTTPClient setAuthorizationHeaderWithUsername:@「username」password:@「password」]; - 我只是嘗試使用setDefaultHeader單獨設置用戶名/密碼,它不起作用。你的建議的確如此。 –

1

我的解決辦法這裏:

// Build a RestKit manager object to look after the restful stuff 
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://test.myserver.com"]];; 

// Hash the GUI input password string and pass the username in plain text 
NSString *md5PW = [umfundiCommon md5:passwordField.text];   
[manager.HTTPClient setAuthorizationHeaderWithUsername:userField.text password:md5PW]; 

RKObjectMapping *WebResponse = [RKObjectMapping mappingForClass:[WSObject class]]; 

     [WebResponse addAttributeMappingsFromDictionary:@{@"id_user":@"id_user", @"datalocation": @"datalocation", @"country":@"country", @"data": @"data", @"login": @"login", @"password": @"password", @"accessLevel": @"accessLevel", @"timestamp": @"timestamp", @"datahash": @"datahash"}]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:WebResponse pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
// Add the above response descriptor to the manager 
[manager addResponseDescriptor:responseDescriptor]; 

// the getObject makes the call using the stuff assembled into the manager Object and drops into either the success or the failure routines. 
[manager getObject:nil path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) 
{ 
NSLog (@"Server WS call success:"); 

NSArray *theresults = [result array];    
for (WSObject *item in theresults) { 
    NSLog(@"datahash=%@",item.datahash); 
    NSLog(@"user_id=%@",item.id_user); 
    } 
} failure:^(RKObjectRequestOperation * operation, NSError * error) 
    { 
    NSLog (@"Server WS call failure: operation: %@ \n\nerror: %@", operation, error); 
    }]; 

........