请帮忙。我是RestKit的新手,并且几天来一直在努力使用RestKit .20.3成功执行一个相当直接的POST。我遵循所有各种样本和说明,并使用了Alexander Edge的教程和RestKit GitHub示例和文档的一部分无济于事。有人可以快速浏览一下我的代码并给我一些提示,告诉我如何使用RestKit .20.3执行成功的POST?我怀疑我的最后一行代码是错误的,因为我的postObject是“nil”但是,我只是不确定。任何提示或建议将不胜感激。
BTW:我在调试日志中收到的错误是“预期状态代码(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];
}
这是我更新的代码。我做了所有的建议mods,现在我收到以下错误:得到500“UserInfo = 0x95c7bd0 {NSLocalizedRecoverySuggestion =错误500:javax.servlet.ServletException:org.codehaus.jackson.map.JsonMappingException:找不到非解串器-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);
}];
答案 0 :(得分:0)
帖子对象不能是nil
,重点是你试图发布该对象。 204响应基本上是确认服务器收到了您的请求但没有工作要做。
您使用的路径不应该是完整的网址。您可以使用managerWithBaseURL
配置管理器,因此路径应该是getDataURL
上的附加部分。
你得到"Expected status code in (400-499), got 204"
,因为你已经配置了一个好的通用错误处理响应描述符(适用于任何东西,因为路径模式是nil
。你还应该为成功案例指定一个响应描述符。创建ticketParentMapping
但从未实际使用它......
答案 1 :(得分:0)
如上所述,POST意味着您传递的对象将根据您指向的URL的RequestDescriptor进行序列化。
无效。 此外,您尚未设置RequestDescriptor