私はrestkitを使用するIOSアプリを作成しています.GETリクエストは問題なく実行でき、正しくマップされますが、restkitでデータベースへのPOSTを実行するのに問題があります. 次のような応答が返ってきます。
`{"errors":{"email":["can't be blank"],"password":["can't be blank"]}}`
オブジェクトが送信されていないような気がします。アプリのデリゲートで、すべてのマッピングを設定するコードを次に示します。
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>>
正しい方向への例またはポイントは驚くべきものです。私はしばらくこれにこだわっています。
さらに情報が必要な場合は、お知らせください。
アップデート
私はそれを機能させることができましたが、正しい方法ではありません.基本的に、私はすべての情報をパラメータを介してそのように送信します.
- (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;
次に、RKObjectMapping の代わりに RKEntityMapping を使用する必要があります。
お役に立てれば。
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];