0

我正在尝试使用 restkit 发布到 API。当我按下保存帖子时,我收到一个错误,显示一个空帖子并警告说:

Expected status code in (200-299), got 422" UserInfo=0xb83ca20 {NSLocalizedRecoverySuggestion={"body":["can't be blank"]}, 

帖子显示在 tableView 中,但我输入的文本没有进入数据库。任何想法为什么会这样?我看到的常见问题是因为人们缺少将数据解析为 json 或者因为他们缺少 responseDescriptor - 但我有这个并且我没有收到 json 错误。它只是不保留我输入的文本。为什么是这样?

我的 savePost 方法是这样的

RKManagedObjectStore *objectStore = [[RKObjectManager sharedManager] managedObjectStore];
Post *post = [NSEntityDescription insertNewObjectForEntityForName:@"Post" inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
[post setBody:self.postTextField.text];
[objectStore.mainQueueManagedObjectContext save:nil];

RKObjectManager *manager = [RKObjectManager sharedManager]; [manager postObject:self path:@"/posts.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Success saving post");
   // [MUser setCurrentUser:self];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failure saving post: %@", error.localizedDescription);
}];

在我的 appDelegate 我有这个设置:

 NSError *error = nil;
    NSURL *modelURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Markofresh" ofType:@"momd"]];
    // NOTE: Due to an iOS 5 bug, the managed object model returned is immutable.
    NSManagedObjectModel *managedObjectModel = [[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL] mutableCopy];
    RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];

    // Initialize the Core Data stack
    [managedObjectStore createPersistentStoreCoordinator];

    NSPersistentStore __unused *persistentStore = [managedObjectStore addInMemoryPersistentStore:&error];
    NSAssert(persistentStore, @"Failed to add persistent store: %@", error);

    [managedObjectStore createManagedObjectContexts];

    // Configure a managed object cache to ensure we do not create duplicate objects
    managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

    // Set the default store shared instance
    [RKManagedObjectStore setDefaultStore:managedObjectStore];

    // Configure the object manager
    RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://fierce.herokuapp.com"]];
    objectManager.managedObjectStore = managedObjectStore;
    [objectManager setRequestSerializationMIMEType:@"application/json"];
    [objectManager setAcceptHeaderWithMIMEType:@"application/json"];


    [RKObjectManager setSharedManager:objectManager];
    //setup Post Entity map
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:@"Post" inManagedObjectStore:managedObjectStore];
    [entityMapping addAttributeMappingsFromDictionary:@{
     @"id":             @"postID",
     @"url":            @"jsonURL",
     @"body":           @"body",
     @"created_at":     @"createdAt"}];
    entityMapping.identificationAttributes = @[ @"postID" ];

    [RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:entityMapping pathPattern:@"/posts.json" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [objectManager addResponseDescriptor:responseDescriptor];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[entityMapping inverseMapping] objectClass:[Post class] rootKeyPath:@"/posts.json"];
    [objectManager addRequestDescriptor:requestDescriptor];

我看到其他人对此很感兴趣,想知道 AFNetworking 是否可能是这种简单方法的更好选择——而且还有更多关于它的文档。我知道我想做的很简单,我不明白是什么导致了空帖子。任何帮助将不胜感激。

4

1 回答 1

0

你应该这样做:

[manager postObject:post path:@"/posts.json" parameters:nil  ...

所以你的问题只是一个错字?


尝试打开映射跟踪日志记录,以便查看发生了什么:

RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

想一想像Charles Proxy这样的东西,可以准确地向您展示发生了什么问题。

于 2013-06-21T22:19:41.913 回答