我是新來restKit,我有幾個問題給你。我 不知道如何發送Post請求使用json/xml到我的網絡 服務和映射傳入的答覆與我的班。任何一個可以給我 幫助。我使用的代碼是這樣的: 在我applicationDelegate我實例化RKObjectManager 提供基本網址:發送帖子請求和地圖回覆對象
RKObjectManager* manager = [RKObjectManager objectManagerWithBaseURL:@"https://example.com/services/"];
// Request params in Dictionary
NSArray *objects = [NSArray arrayWithObjects: email, password,
nil];
NSArray *keys = [NSArray arrayWithObjects:@"username",
@"password", nil];
NSDictionary *params = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
NSLog(@"Manager: %@", [RKObjectManager
sharedManager].description);
// User object Mapping
RKObjectMapping* userMapping = [RKObjectMapping mappingForClass:
[User class]];
[userMapping mapKeyPath:@"userName" toAttribute:@"userName"];
[userMapping mapKeyPath:@"lastName" toAttribute:@"lastName"];
[userMapping mapKeyPath:@"active" toAttribute:@"active"];
[[RKObjectManager sharedManager].mappingProvider
setMapping:userMapping forKeyPath:@"user"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/
login" delegate:self];
在一個職位發送到/登錄服務器應該發回一個有效的JSON 然後映射該JSON我的用戶類。
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:
(NSArray*)objects {
RKLogInfo(@"Load collection of Articles: %@", objects);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:
(NSError *)error
{
NSLog(@"Objecy Loader failed: %@", error);
}
- (void)request:(RKRequest *)request didFailLoadWithError:(NSError
*)error
{
NSLog(@"Request failed");
}
- (void)request:(RKRequest*)request didLoadResponse:
(RKResponse*)response {
if ([request isGET]) {
// Handling GET /foo.xml
if ([response isOK]) {
// Success! Let's take a look at the data
NSLog(@"Retrieved XML: %@", [response bodyAsString]);
}
} else if ([request isPOST]) {
// Handling POST /other.json
if ([response isXML]) {
///NSLog(@"Seng a JSON request:! \n %@", [request
HTTPBodyString]);
NSLog(@"Got a responce! \n %@", [response bodyAsString]);
}
} else if ([request isDELETE]) {
// Handling DELETE /missing_resource.txt
if ([response isNotFound]) {
NSLog(@"The resource path '%@' was not found.", [request
resourcePath]);
}
}
}
當我執行它的objectLoader方法不會被觸發,我restkit的 理解是,當
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/login"
delegate:self];
執行他們應該叫什麼?任何幫助表示讚賞:)
是的,這個方法的類正在實現上面編輯的RKObjectLoaderDelegate – greenLizard 2012-01-18 17:08:02
以及更多的想法 – glogic 2012-01-18 17:23:14