0

我成功地 POST 到一个 RESTful 服务,但它正在发回以下 json 响应;

 T restkit.network:RKObjectLoader.m:202 bodyAsString: {"Successful":true,"Message":"We have received 9138 message(s)","Data":null}

(来自 RestKit 调试器/记录器)。如您所见,该 JSON 中没有根密钥。现在 RestKit 在尝试使用以下代码映射它时会抱怨;

RKObjectMapping *serverResponseMapping = [RKObjectMapping mappingForClass:[ServerReply class]];
[serverResponseMapping setRootKeyPath:@""]; //tried without this and with a value but no change!
[serverResponseMapping mapKeyPath:@"Success" toAttribute:@"success"];
[serverResponseMapping mapKeyPath:@"Message" toAttribute:@"message"];
[serverResponseMapping mapKeyPath:@"Data" toAttribute:@"data"];
[[manager mappingProvider] addObjectMapping:serverResponseMapping];

尝试映射时,我从 Restkit 收到以下错误;

W restkit.object_mapping:RKObjectMapper.m:87 Adding mapping error: Could not find an object mapping for keyPath: ''

只是为了突出 -[serverResponseMapping setRootKeyPath:@""]; //尝试没有这行代码,也有一个值但没有变化!

谁能告诉我如何解决这个问题,因为我认为我无法更改服务器?

谢谢

4

1 回答 1

1

终于解决了它,所以我想我会在其他人发现它有用的情况下更新它。如果使用 RKObjectManager postObject 消息的块构造,则可以在 ObjectLoader 中指定返回消息的接收对象;

[manager postObject:thePostMsgObj usingBlock:^(RKObjectLoader *loader){
    loader.targetObject = theResponseObj;
    loader.delegate = self;
    loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[response Class]];
}];
于 2012-11-12T09:32:36.197 回答