我是restKit的新手,我有几个问题要问你。一世 无法理解如何使用json / xml将Post请求发送到我的网站 服务并将传入的回复映射到我的班级。任何人都可以给予 我帮忙。我使用的代码是这样的: 在我的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];
当帖子发送到/ login时,服务器应该发回一个有效的json 然后将该json映射到我的User类。
- (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];
执行了吗?任何帮助表示赞赏:)
答案 0 :(得分:2)
好吧,您甚至都没有将数据发送到服务器。几天前,我写了一个解释how to post a NSDictionary (as JSON) to a server using RestKit的片段。
答案 1 :(得分:1)
你是否记得将restkit的委托设置为该用户类(或者你的代表被捕获的地方)
@interface User : NSObject<RKObjectLoaderDelegate>{
}
你可能做过,但值得一提的是:P
并且在这一行中传递的self是那个类?
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/
login" delegate:self];
我认为添加映射的新方法是为帖子设置
[sharedManager.mappingProvider addObjectMapping:userMapping];
[sharedManager.mappingProvider setMapping:userMapping forKeyPath:@"/somepath"];
[sharedManager.mappingProvider setSerializationMapping:[userMapping inverseMapping] forClass:[User class]];
// Must set the router up to handle posts
[sharedManager.router routeClass:[User class] toResourcePath:@"/api/users" forMethod:RKRequestMethodPOST];
再次编辑:使用像这样的加载器创建一个帖子。可能会为你的行为而全部杀死,但看起来不能伤害
RKObjectLoader* loader = [RKObjectLoader loaderWithResourcePath:url objectManager:self.objectManager delegate:self.currentDelegate];
loader.method = RKRequestMethodPOST;
loader.sourceObject = self;
loader.targetObject = self;
loader.serializationMIMEType = self.objectManager.serializationMIMEType;
loader.serializationMapping = [self.objectManager.mappingProvider serializationMappingForClass:self.mappingClass];
loader.objectMapping = [self.objectManager.mappingProvider objectMappingForClass:self.mappingClass];
[loader send]; //<<the actual post