gpt4 book ai didi

ios - RestKit 将对象 POST 到服务器,但服务器收到空的 GET 请求

转载 作者:行者123 更新时间:2023-11-29 04:11:50 26 4
gpt4 key购买 nike

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

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

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];

最佳答案

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

关于ios - RestKit 将对象 POST 到服务器,但服务器收到空的 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com