请帮忙。我对 RestKit 很陌生,并且几天来一直在努力使用 RestKit .20.3 成功执行相当直接的 POST。我遵循所有各种示例和说明,并使用了 Alexander Edge 教程的一部分以及 RestKit GitHub 示例和文档,但无济于事。有人可以快速浏览一下我的代码并给我一些提示,告诉我如何使用 RestKit .20.3 执行成功的 POST 吗?我怀疑我的最后一行代码有问题,因为我的 postObject 是“nil”,但是我不确定。任何提示或建议将不胜感激。
顺便说一句:我在调试日志中收到的错误是“(400-499)中的预期状态代码,得到 204”
- (void)setupRestKitTutorial{
NSLog(@"setupRestKitTutorial - Start");
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:getDataURL]];
//[manager setRequestSerializationMIMEType:RKMIMETypeJSON];
//[[manager HTTPClient] setDefaultHeader:@"X-Parse-REST-API-Key" value:@"inquiryData"];
//[[manager HTTPClient] setDefaultHeader:@"X-Parse-Application-Id" value:@"inquiryData"];
//Set up Request Header
[[manager HTTPClient] setDefaultHeader:@"clientAccount" value:@"6"];
[[manager HTTPClient] setDefaultHeader:@"docType" value:@"T"];
[[manager HTTPClient] setDefaultHeader:@"userId" value:@"uid01"];
[[manager HTTPClient] setDefaultHeader:@"class" value:headerClass];
[[manager HTTPClient] setDefaultHeader:@"clientCode" value:@"02"];
[[manager HTTPClient] setDefaultHeader:@"requestType" value:@"submit"];
[[manager HTTPClient] setDefaultHeader:@"token" value:@"f3a6a4dc1c8f77d79a1a1f6a5d439933"];
[[manager HTTPClient] setDefaultHeader:@"documentNum" value:@"439299827"];
[[manager HTTPClient] setDefaultHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
//End setup Request Header
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
pathPattern:nil
keyPath:@"error"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[manager addResponseDescriptorsFromArray:@[errorDescriptor]];
NSDictionary *parentObjectMapping = @{
@"inquiryStatus" : @"inquiryStatus",
@"entityNo" : @"entityNo",
@"docType" : @"docType",
@"docNumber" : @"docNumber",
@"mvNumber" : @"mvNumber",
@"totalAmount" : @"totalAmount"
};
RKEntityMapping *ticketParentMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([TicketParent class]) inManagedObjectStore:manager.managedObjectStore];
ticketParentMapping.identificationAttributes = @[ @"entityNo" ];
[ticketParentMapping addAttributeMappingsFromDictionary:@{
@"name" : @"name"
}];
[ticketParentMapping addAttributeMappingsFromDictionary:parentObjectMapping];
/**
Complete Core Data stack initialization
*/
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TicketParent.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} 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];
[manager postObject:nil path:@"http://www.mytesturl.com/jsonservices" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
NSLog(@"We object mapped the response with the following result: %@", result);
} failure:nil];
}
这是我更新的代码。我做了所有建议的模组,现在我收到以下错误:got 500" UserInfo=0x95c7bd0 {NSLocalizedRecoverySuggestion=Error 500: javax.servlet.ServletException: org.codehaus.jackson.map.JsonMappingException: Can not find a deserializer for non -concrete Map type [map type; class javax.ws.rs.core.MultivaluedMap, [simple type, class java.lang.String] -> [simple type, class java.lang.String]] 关于修复的任何想法?
- (void)setupRestKitTutorial{
NSLog(@"setupRestKitTutorial - Start");
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:getDataURL]];
[manager setRequestSerializationMIMEType:RKMIMETypeJSON];
[manager setAcceptHeaderWithMIMEType:@"application/x-www-form-urlencoded"];
[manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[RKObjectManager setSharedManager:manager];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"application/x-www-form-urlencoded"];
RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromDictionary:@{@"clientAccount" : @"clientAccount",
@"docType" : @"docType",
@"userId" : @"userId",
@"class1" : @"class1",
@"clientCode" : @"clientCode",
@"requestType" : @"requestType",
@"token" : @"token",
@"documentNum" : @"documentNum"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping objectClass:[RequestInquiry class] rootKeyPath:nil method:RKRequestMethodPOST];
[manager addRequestDescriptor:requestDescriptor];
[[manager HTTPClient] setDefaultHeader:@"X-Parse-REST-API-Key" value:@"inquiryData"];
[[manager HTTPClient] setDefaultHeader:@"X-Parse-Application-Id" value:@"inquiryData"];
//Set up Request Header
[[manager HTTPClient] setDefaultHeader:@"clientAccount" value:@"6"];
[[manager HTTPClient] setDefaultHeader:@"docType" value:@"T"];
[[manager HTTPClient] setDefaultHeader:@"userId" value:@"uid01"];
[[manager HTTPClient] setDefaultHeader:@"class1" value:headerClass];
[[manager HTTPClient] setDefaultHeader:@"clientCode" value:@"02"];
[[manager HTTPClient] setDefaultHeader:@"requestType" value:@"submit"];
[[manager HTTPClient] setDefaultHeader:@"token" value:@"f3a6a4dc1c8f77d79a1a1f6a5d439933"];
[[manager HTTPClient] setDefaultHeader:@"documentNum" value:@"439299827"];
//[[manager HTTPClient] setDefaultHeader:@"contenttype" value:@"application/x-www-form-urlencoded"];
//End setup Request Header
NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;
RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]];
[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]];
RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping
pathPattern:nil
keyPath:@"error"
//statusCodes:[NSIndexSet indexSetWithIndex:200]];
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)];
[manager addResponseDescriptorsFromArray:@[errorDescriptor]];
NSDictionary *parentObjectMapping = @{
@"inquiryStatus" : @"inquiryStatus",
@"entityNo" : @"entityNo",
@"docType" : @"docType",
@"docNumber" : @"docNumber",
@"mvNumber" : @"mvNumber",
@"totalAmount" : @"totalAmount"
};
RKEntityMapping *ticketParentMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([TicketParent class]) inManagedObjectStore:manager.managedObjectStore];
ticketParentMapping.identificationAttributes = @[ @"entityNo" ];
[ticketParentMapping addAttributeMappingsFromDictionary:@{
@"mvType" : @"mvType"
}];
[ticketParentMapping addAttributeMappingsFromDictionary:parentObjectMapping];
/**
Complete Core Data stack initialization
*/
[managedObjectStore createPersistentStoreCoordinator];
NSString *storePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"TicketParent.sqlite"];
NSError *error;
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:storePath fromSeedDatabaseAtPath:nil withConfiguration:nil options:@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES} 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];
// Request Object with json paramaters
RequestInquiry *requestInquiry = [[RequestInquiry alloc] init];
requestInquiry.docType = @"T";
requestInquiry.documentNum = @"439299827";
requestInquiry.clientAccount = @"6";
requestInquiry.clientCode = @"02";
requestInquiry.token = @"f3a6a4dc1c8f77d79a1a1f6a5d439933";
requestInquiry.requestType = @"submit";
requestInquiry.userId = @"uid01";
requestInquiry.class1 = @"class com.rest.client.JsonRequest";
[manager postObject:requestInquiry path:@"/RestIVR/restservices/ticketservices/jinquiry" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *ticketParentMapping)
{
RKLogError(@"%@",ticketParentMapping);
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
RKLogError(@"%@",error);
}];