在使用 RestKit 的发布请求期间,我无法将响应映射回对象。
这是代码:
要求:
// mapping for the response. response is an object: {"response":"message","success":bool}
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GenericResponse class]];
[responseMapping addAttributeMappingsFromArray:@[@"success",@"response"]];
responseMapping.setDefaultValueForMissingAttributes = YES;
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping pathPattern:@"/authenticate" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
// mapping for the request body
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"username", @"password"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[LoginCriteria class] rootKeyPath:nil];
// set up the request
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:8080"]];
[manager addResponseDescriptor:responseDescriptor];
[manager addRequestDescriptor:requestDescriptor];
[manager setRequestSerializationMIMEType:@"application/json"];
// set up the LoginCriteria object
LoginCriteria* loginCriteria = [LoginCriteria new];
loginCriteria.password = @"test";
loginCriteria.username = @"test";
// make the request
[manager postObject:loginCriteria path:@"/authenticate" parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
GenericResponse *genericResponse = (GenericResponse*)mappingResult;
NSLog(@"logged in: %@", [mappingResult array]);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"login failed");
}];
通用响应.h:
@interface GenericResponse : NSObject
@property (nonatomic) Boolean* success;
@property (nonatomic, copy) NSString* response;
@end
日志:
2012-12-17 15:44:22.890 Radiuus[8221:1703] T restkit.network:RKHTTPRequestOperation.m:139 POST 'http://localhost:8080/authenticate':
request.headers={
Accept = "application/json";
"Accept-Language" = "en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8";
"Content-Type" = "application/json; charset=utf-8";
"User-Agent" = "Radiuus/1.0 (iPhone Simulator; iOS 6.0; Scale/1.00)";
}
request.body=(null)
2012-12-17 15:44:23.244 Radiuus[8221:5d0b] T restkit.network:RKHTTPRequestOperation.m:156 POST 'http://localhost:8080/authenticate' (200):
response.headers={
"Content-Type" = "application/json";
Date = "Mon, 17 Dec 2012 20:44:23 GMT";
Server = "Apache-Coyote/1.1";
"Transfer-Encoding" = Identity;
}
response.body={"response":"authentication succeeded","success":true}
2012-12-17 15:44:23.246 Radiuus[8221:4f03] W restkit.object_mapping:RKMapperOperation.m:76 Adding mapping error: Expected an object mapping for class of type 'LoginCriteria', provider returned one for 'GenericResponse'
从日志中,对我来说奇怪的是,RestKit 似乎期望反序列化对 LoginCriteria 对象的响应,但是当它正确获取 GenericResponse 对象时“失败”,这当然是正确的。
任何帮助是极大的赞赏!