Restkit Post Nicht senden Objekt
Ich bin erstellen einer IOS-app, die verwendet restkit und ich bin in der Lage zu tun, um Anfragen kein problem und es Karten richtig und alles, aber ich habe Probleme damit, restkit, um einen BEITRAG an die Datenbank. Es wird immer eine Antwort, die sagt:
`{"errors":{"email":["can't be blank"],"password":["can't be blank"]}}`
Habe ich das Gefühl, dass das Objekt gerade nicht gesendet wird. Hier ist mein code, um alle mappings, die in meinem app-delegate.
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];
und hier ist mein post-Anforderung:
- (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);
}];
}
Hier sind die logs, die ich immer bin:
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>>
Irgendein Beispiel, oder zeigen Sie in die richtige Richtung wäre toll. Habe ich fest auf dieser für eine Weile.
Wenn Sie weitere Informationen benötigen, lasst es mich einfach wissen.
Update
Ich war in der Lage, es zu arbeiten, aber nicht der richtige Weg.Grundsätzlich habe ich nur senden Sie alle Informationen, die durch die params so gerne.
- (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];
}];
}
Jemand irgendwelche Ideen auf, wie es zu tun richtig?
Update für Vishal
Für die Verwendung von coredata müssen Sie zum erstellen eines Verwalteten Objekt Speichern, wie dies...
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;
Dann sind Sie gehen zu müssen, verwenden RKEntityMapping statt RKObjectMapping
Hoffe, das hilft.
Update 2 für Vishal
[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];
- Hallo, Haben Sie in der Lage gewesen, dies mit einer ObjectStore? Mein Objekt-mappings sind ähnlich wie Ihr, aber versuche, mit diesem zusammen mit einem ObjectStore und ich bekomme die folgende Fehlermeldung: Sie müssen einen managedObjectStore. Aufrufen mappingForClass:inManagedObjectStore: statt.
- Hey das update habe ich gerade getan hast. Es dauerte eine lange Zeit, um es herauszufinden, aber ich habe es endlich. Lassen Sie mich wissen, wenn Sie etwas anderes benötigen.
- Danke @dostrander. Jedoch, sogar danach für meine Entity Mapping sagt es muss eine Managed-Object-Store. Meine Entity-Mappings sind ähnlich wie bei Ihnen.
- Sie haben das Stück code, dass ich einfach setzen?
- Danke @dostrander. Ich habe es behoben, indem die folgenden: RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore: managedObjectStore];
Du musst angemeldet sein, um einen Kommentar abzugeben.
War ich den Umgang mit dem gleichen problem. Sie vermissen Anfrage Zuordnung. Du bist nur das beschreiben Antwort.
Beispiel-code:
NSInvalidArgumentException, reason: RKRequestDescriptor objects must be initialized with a mapping whose target class is NSMutableDictionary, got User
RKObjectMapping *userRequestMapping = [RKObjectMapping requestMapping];
. Sie können hinzufügen dictionary-Zuordnung danach: `[userRequestMapping addAttributeMappingsFromDictionary:userMappingDictionary];`[[Data mapping] inverseMapping]