我正在使用RKXMLReaderSerialization并尝试将XML响应从我的服务器映射到对象。它成功了,但映射结果中的对象只有空值。
这是我试图从我的服务器映射的text / xml响应:
<Provision version="1.0">
<FileInfoWrapper>
<FileUrl>SomeFile.zip</FileUrl>
<FileName>SomeFile.zip</FileName>
<FileSha1>oi7NK/rFLL6dXqcu7ahaNfKsGkE=</FileSha1>
<FileSize>52220448</FileSize>
<Version>13</Version>
<Vital>true</Vital>
</FileInfoWrapper>
</Provision>
这是我的模型对象:
@interface FileInfoWrapper : NSObject
@property NSString *fileUrl;
@property NSString *fileName;
@property NSString *fileSha1;
@property long fileSize;
@property NSString *version;
@property BOOL vital;
@end
我添加了RKXMLReaderSerialization:
[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeTextXML];
[[self objectManager] setAcceptHeaderWithMIMEType:@"application/json,text/xml"];
我认为我正确设置了映射和响应描述符:
RKObjectMapping *fileInfoMapping = [RKObjectMapping mappingForClass:[FileInfoWrapper class]];
[fileInfoMapping addAttributeMappingsFromDictionary:@{
@"FileUrl": @"fileUrl",
@"FileName": @"fileName",
@"FileSha1": @"fileSha1",
@"FileSize": @"fileSize",
@"Version": @"version",
@"Vital": @"vital"}];
RKResponseDescriptor *fileInfoResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:fileInfoMapping
method:RKRequestMethodGET
pathPattern:nil
keyPath:@"Provision.FileInfoWrapper"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[self objectManager] addResponseDescriptor:fileInfoResponseDescriptor];
但是当我打电话时:
[[self objectManager] getObjectsAtPath:@"static/download/Installer.info.xml"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"****Success!****");
NSLog(@"mappingResult: %@", mappingResult);
FileInfoWrapper *fileInfo = [mappingResult firstObject];
NSLog(@"URL: %@", [fileInfo fileUrl]);
NSLog(@"Name: %@", [fileInfo fileName]);
NSLog(@"SHA1: %@", [fileInfo fileSha1]);
NSLog(@"Size: %lx", [fileInfo fileSize]);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"****Failure!****");
}];
所有值均为null:
I restkit.network:RKObjectRequestOperation.m:180 GET 'http://example.com/static/download/Installer.info.xml'
I restkit.network:RKObjectRequestOperation.m:250 GET 'http://example.com/static/download/Installer.info.xml' (200 OK / 1 objects) [request=0.1080s mapping=0.0060s total=0.1226s]
****Success!****
mappingResult: <RKMappingResult: 0x10064b190, results={
"Provision.FileInfoWrapper" = "<FileInfoWrapper: 0x10065f780>";
}>
URL: (null)
Name: (null)
SHA1: (null)
Size: 0
我做错了什么?
更新:我启用了RestKit / ObjectMapping日志记录并获得了一些其他信息:
D restkit.object_mapping:RKMapperOperation.m:377 Executing mapping operation for representation: {
Provision = {
FileInfoWrapper = {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
}
and targetObject: (null)
T restkit.object_mapping:RKMapperOperation.m:320 Examining keyPath 'Provision.FileInfoWrapper' for mappable content...
D restkit.object_mapping:RKMapperOperation.m:300 Found mappable data at keyPath 'Provision.FileInfoWrapper': {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
}
D restkit.object_mapping:RKMapperOperation.m:231 Asked to map source object {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
} with mapping <RKObjectMapping:0x10025e6b0 objectClass=FileInfoWrapper propertyMappings=(
"<RKAttributeMapping: 0x10025ebc0 FileSha1 => fileSha1>",
"<RKAttributeMapping: 0x10025ed90 FileName => fileName>"
…
)>
D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
T restkit.object_mapping:RKMappingOperation.m:953 Performing mapping operation: <RKMappingOperation 0x10068f730> for 'FileInfoWrapper' object. Mapping values from object {
FileName = {
text = "SomeFile.zip";
};
FileSha1 = {
text = "oi7NK/rFLL6dXqcu7ahaNfKsGkE=";
};
…
} to object <FileInfoWrapper: 0x10068f290> with object mapping (null)
T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'FileUrl' to 'fileUrl'
T restkit.object_mapping:RKMappingOperation.m:431 Found transformable value at keyPath 'FileUrl'. Transforming from type '__NSDictionaryM' to 'NSString'
D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'FileInfoWrapper': {
fileName = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = fileName;
};
fileSha1 = {
isPrimitive = 0;
keyValueCodingClass = NSString;
name = fileSha1;
};
…
}
T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'FileUrl to keyPath 'fileUrl' -- value is unchanged ((null))
T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'FileSha1' to 'fileSha1'
T restkit.object_mapping:RKMappingOperation.m:431 Found transformable value at keyPath 'FileSha1'. Transforming from type '__NSDictionaryM' to 'NSString'
T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'FileSha1 to keyPath 'fileSha1' -- value is unchanged ((null))
…
答案 0 :(得分:3)
您似乎必须从文本节点进行映射。这篇文章有同样的问题:
RestKit 0.20-pre3 with RKXMLReaderSerialization and XMLReader
将映射更改为:
[fileInfoMapping addAttributeMappingsFromDictionary:@{
@"FileUrl.text": @"fileUrl",
@"FileName.text": @"fileName",
@"FileSha1.text": @"fileSha1",
@"FileSize.text": @"fileSize",
@"Version.text": @"version",
@"Vital.text": @"vital",}];
做到了。
答案 1 :(得分:2)
如果您期望XML,则不应将accept标头设置为JSON。我也不认为你可以设置这样的逗号分隔列表(虽然不是100%肯定)。如果服务器因接受标头而发送JSON,那么RestKit会对响应感到满意,但实际上可能无法进行任何映射(看起来就像你看到的那样)。
启用跟踪日志记录以获取有关响应和映射的更多信息:
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
您的映射代码看起来确实正确。