0

当我postObject在 RestKit 中使用该方法时,日志显示一切正常(见下文)。

但是在服务器上,请求被接收为GET并且请求正文为空,而不是POST包含序列化对象的 a。

2013-01-12 14:40:08.860 AppName[3953:c07] I restkit.network:RKHTTPRequestOperation.m:152 POST 'http://urlgoeshere.com'

下面是我用于POST和 RestKit 初始化的代码。我正在使用 RestKit 0.20.0-pre6。

POST 对象到服务器

// CREATE THE MANAGED OBJECT, AND SAVE IT
RKManagedObjectStore *objectStore = [[RKObjectManager sharedManager] managedObjectStore];
Message *msg = [NSEntityDescription insertNewObjectForEntityForName:@"Message" inManagedObjectContext:objectStore.mainQueueManagedObjectContext];
msg.note = @"This is a sample message.";
msg.dateCreated = [NSDate date];
[objectStore.mainQueueManagedObjectContext save:nil];

// BUILD THE REQUEST MAPPING & REQUEST DESCRIPTOR
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
 @"messageId": @"id",
 @"dateCreated": @"date_created",
 }];
[requestMapping addAttributeMappingsFromArray:@[ @"note" ]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Message class] rootKeyPath:@"message"];
[[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];

// SEND POST REQUEST TO SERVER
[[RKObjectManager sharedManager] postObject:msg
                                       path:@"/path/to/messages"
                                 parameters:[NSDictionary dictionaryWithObject:[(AppDelegate*)[[UIApplication sharedApplication] delegate] userId] forKey:@"user_id"]
                                    success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                        NSLog(@"success!");
                                    }
                                    failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                        NSLog(@"failure.\n\n%@", [error description]);
                                    }
 ];

App Delegate 中的 RestKit 初始化

NSURL *baseURL = [NSURL URLWithString:@"http://urlgoeshere.com"];
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseURL];

// Initialize managed object store
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
objectManager.managedObjectStore = managedObjectStore;

// Setup our object mappings    
RKEntityMapping *messageMapping = [RKEntityMapping mappingForEntityForName:@"Message" inManagedObjectStore:managedObjectStore];
messageMapping.identificationAttributes = @[ @"messageId" ];
[messageMapping addAttributeMappingsFromDictionary:@{
 @"id": @"messageId",
 @"date_created": @"dateCreated",
 }];
[messageMapping addAttributeMappingsFromArray:@[ @"note" ]];

[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"DBNameHere.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
NSAssert(persistentStore, @"Failed to add persistent store with error: %@", error);

// Create the managed object contexts
[managedObjectStore createManagedObjectContexts];

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

1 回答 1

1

你在和什么样的服务器说话?我怀疑您的服务器正在执行重定向或其他操作。RestKit 使用 NSURLConnection 发出请求,因此问题极不可能出现在客户端,因为您在日志中看到的报告是直接从 NSURLRequest 读取的。

于 2013-01-12T21:22:21.540 回答