我正在创建一个使用restkit的IOS应用程序,我能够执行GET请求没有问题,并且它正确地映射了一切但是,我无法让restkit对数据库进行POST。它正在回应说:
`{"errors":{"email":["can't be blank"],"password":["can't be blank"]}}`
我感觉对象没有被发送。这是我在我的app委托中设置所有映射的代码。
RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
RKObjectMapping *dogMapping = [RKObjectMapping mappingForClass:[Dog class]];
RKObjectMapping *posttimeMapping = [RKObjectMapping mappingForClass:[PostTime class]];
RKObjectMapping *postMapping = [RKObjectMapping mappingForClass:[Post class]];
RKObjectMapping *activityMapping = [RKObjectMapping mappingForClass:[Activity class]];
// set up which attributes go where
[userMapping addAttributeMappingsFromDictionary:[User propertyMappings]];
[dogMapping addAttributeMappingsFromDictionary:[Dog propertyMappings]];
[posttimeMapping addAttributeMappingsFromDictionary:[PostTime propertyMappings]];
[postMapping addAttributeMappingsFromDictionary:[Post propertyMappings]];
[activityMapping addAttributeMappingsFromDictionary:[Activity propertyMappings]];
// set up relationships
[userMapping addRelationshipMappingWithSourceKeyPath:@"dogs" mapping:dogMapping];
[dogMapping addRelationshipMappingWithSourceKeyPath:@"timeline" mapping:posttimeMapping];
[posttimeMapping addRelationshipMappingWithSourceKeyPath:@"posts" mapping:postMapping];
[postMapping addRelationshipMappingWithSourceKeyPath:@"activities" mapping:activityMapping];
// what to print
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("Restkit/Network", RKLogLevelDebug);
// set up paths
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2x
RKResponseDescriptor *userDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern: @"/users/1.json" keyPath:@"" statusCodes:statusCodes];
RKResponseDescriptor *signupDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern:@"/users.json" keyPath:@"" statusCodes:statusCodes];
[manager addResponseDescriptorsFromArray:@[ userDescriptor, signupDescriptor ]];
[RKObjectManager setSharedManager:manager];
这是我的帖子请求:
- (void) post{
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager postObject:self path:@"/users.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"successful post");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"failed post %@", error);
NSLog(@"%@",operation.description);
NSLog(@"%@",operation.HTTPRequestOperation.description);
}];
}
以下是我得到的日志:
2012-12-19 11:23:15.657 Dogapp[6353:4303] T restkit.network:RKHTTPRequestOperation.m:139 POST 'http://localhost:3000/users.json':
request.headers={
Accept = "application/json";
"Accept-Language" = "en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8";
"Content-Type" = "application/x-www-form-urlencoded; charset=utf-8";
"User-Agent" = "Dogapp/1.0 (iPhone Simulator; iOS 6.0; Scale/1.00)";
}request.body=(null)
2012-12-19 11:23:15.680 Dogapp[6353:190b] T restkit.network:RKHTTPRequestOperation.m:156 POST 'http://localhost:3000/users.json' (422):
response.headers={
"Cache-Control" = "no-cache";
Connection = "Keep-Alive";
"Content-Length" = 69;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 19 Dec 2012 16:23:15 GMT";
Server = "WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)";
"Set-Cookie" = "_cms_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTRjMTdjZDNiOGVmNDY0MGFjY2NiZWJjNTUwOGQxZDU3BjsAVA%3D%3D--d553a2616530bbe5346fdafc4d6fca81ecc6f6f7; path=/; HttpOnly";
"X-Request-Id" = 5edef73ca7b5ddbc4e510e9799835324;
"X-Runtime" = "0.007666";
"X-Ua-Compatible" = "IE=Edge";
}
response.body={"errors":{"email":["can't be blank"],"password":["can't be blank"]}}
2012-12-19 11:23:15.682 Dogapp[6353:c07] failed post Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Unable to find any mappings for the given content" UserInfo=0x95bcb20 {DetailedErrors=(
), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null}
2012-12-19 11:23:15.683 Dogapp[6353:c07] mappingResult: <RKObjectRequestOperation: 0x75e73b0, state: Failed, isCancelled=NO, request: <NSMutableURLRequest http://localhost:3000/users.json>, response: <NSHTTPURLResponse: 0x75f4580 statusCode=422 MIMEType=application/json length=69>>
2012-12-19 11:23:15.683 Dogapp[6353:c07] <RKObjectRequestOperation: 0x75e73b0, state: Failed, isCancelled=NO, request: <NSMutableURLRequest http://localhost:3000/users.json>, response: <NSHTTPURLResponse: 0x75f4580 statusCode=422 MIMEType=application/json length=69>>
2012-12-19 11:23:15.683 Dogapp[6353:c07] <RKHTTPRequestOperation: 0x75e78e0, state: isFinished, cancelled: NO request: <NSMutableURLRequest http://localhost:3000/users.json>, response: <NSHTTPURLResponse: 0x75f4580>>
任何正确方向的例子或观点都会令人惊叹。我已经坚持了一段时间。
如果您需要更多信息,请告诉我。
我能够让它工作但不是正确的方法。基本上我只是通过params发送所有信息。
- (void) post{
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager postObject:self path:@"/users.json" parameters:@{@"user[email]" : self.email, @"user[password]" : self.password} success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Successful Post!");
[User setCurrentUser:self];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"Okay!" otherButtonTitles: nil];
[alert show];
}];
}
任何人都有任何想法如何正确地做到这一点?
Vishal更新 要使用coredata,您必须创建一个像这样的托管对象库...
RKObjectManager *manager = [RKObjectManager managerWithBaseURL: [NSURL URLWithString:@"http://my_url.com"]];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyDataModel" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;
然后你将不得不使用RKEntityMapping而不是RKObjectMapping
希望这有帮助。
Vishal的更新2
[managedObjectStore createPersistentStoreCoordinator];
NSError *error;
BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (! persistentStore) {
RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}
[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
答案 0 :(得分:26)
我正在处理同样的问题。您缺少请求映射。你只是在描述回应。
示例代码:
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping
objectClass:[User class] rootKeyPath:nil];
[objectManager addRequestDescriptor: requestDescriptor];