在RestKit 0.20中映射对象时出错

时间:2013-05-30 06:45:48

标签: ios restkit-0.20

我正在研究Rest Kit 0.2o,我正在使用一些来自Web的Json格式的示例URL,响应如下。我正在使用对象映射。我按照以下链接作为参考 “https://github.com/RestKit/RestKit/wiki/Object-Mapping”我在链接中使用了几乎相同的响应。

{
  "geonames": [
    {
      "fcodeName":"capital of a political entity",
      "toponymName":"Mexico City",
      "countrycode":"MX",
      "fcl":"P",
      "fclName":"city, village,...",
      "name":"Mexiko-Stadt",
      "wikipedia":"en.wikipedia.org/wiki/Mexico_City",
      "lng":-99.12766456604,
      "fcode":"PPLC",
      "geonameId":3530597,
      "lat":19.428472427036,
      "population":12294193
    },
    {
      "fcodeName":"capital of a political entity",
      "toponymName":"Manila",
      "countrycode":"PH",
      "fcl":"P",
      "fclName":"city,village,...",
      "name":"Manila",
      "wikipedia":"en.wikipedia.org/wiki/Manila",
      "lng":120.9822,
      "fcode":"PPLC",
      "geonameId":1701668,
      "lat":14.6042,
      "population":10444527
    }]
}

以下是代码

RKObjectMapping *newsMapping = [RKObjectMapping mappingForClass:[News class]];
[newsMapping addAttributeMappingsFromDictionary:@{
     @"fcodeName": @"fcodeName",
     @"toponymName": @"toponymName",
     @"countrycode": @"countrycode",
     @"fcl": @"fcl",
     @"fclName": @"fclName",
     @"name":@"name",
     @"wikipedia":@"wikipedia",
     @"lng":@"lng",
     @"fcode":@"fcode",
     @"geonameId":@"geonameId",
     @"lat":@"lat",
     @"population":@"population",
}];

RKResponseDescriptor* responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:newsMapping
                                                                                   pathPattern:nil
                                                                                       keyPath:@"newsMapping" 
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

NSURL* url = [[NSURL alloc]initWithString:@"http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo"];

NSURLRequest *request = [NSURLRequest requestWithURL:url];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];

[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Articles: %@", mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    //  RKLogError(@"Operation failed with error: %@", error);
    NSLog(@"THIS IS TEST %@",error);
}];

[objectRequestOperation start];

“新闻”是我正在使用的课程,我使用的 KeyPath “geonames”

@interface News : NSObject

@property(nonatomic,copy)NSString* fcodeName;
@property(nonatomic,copy)NSString* toponymName;
@property(nonatomic,copy)NSString* countrycode;
@property(nonatomic,copy)NSString* fcl;
@property(nonatomic,copy)NSString* fclName;
@property(nonatomic,copy)NSString* name;
@property(nonatomic,copy)NSString* wikipedia;
@property(nonatomic,copy)NSString* lng;
@property(nonatomic,copy)NSString* fcode;
@property(nonatomic,copy)NSString* geonameId;
@property(nonatomic,copy)NSString* lat;
@property(nonatomic,copy)NSString* population;

完成所有这些后,我得到以下错误

2013-05-30 12:06:14.376 TestApp[7140:11603] I restkit.network:RKObjectRequestOperation.m:174 GET 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo'
2013-05-30 12:06:15.477 TestApp[7140:12b07] E restkit.network:RKObjectRequestOperation.m:236 Test GET 'http://api.geonames.org/citiesJSON?north=44.1&south=-9.9&east=-22.4&west=55.2&lang=de&username=demo' (200 OK / 0 objects) [request=1.0969s mapping=0.0000s total=1.1056s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7675f20 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: newsMapping
The representation inputted to the mapper was found to contain nested object representations at the following key paths: status
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}
2013-05-30 12:06:15.477 TestApp[7140:14d03] blockk
2013-05-30 12:06:15.478 TestApp[7140:14d03] erorrrrrr next ifr
2013-05-30 12:06:15.478 TestApp[7140:14d03] erorrrrrr next ifjhkr
2013-05-30 12:06:15.478 TestApp[7140:11603] THIS IS TEST Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo=0x7675f20 {DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: newsMapping
The representation inputted to the mapper was found to contain nested object representations at the following key paths: status
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}

实际上是错误的,它在最后一行显示keyPath为null但是在浏览器中我得到了上面提到的响应。

请帮助

1 个答案:

答案 0 :(得分:6)

您的响应描述符密钥路径错误。改为:

RKResponseDescriptor* responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:newsMapping pathPattern:nil keyPath:@"geonames" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

您目前看到的错误似乎是因为实际返回的JSON是:

{"status":{"message":"the deaily limit of 30000 credits for demo has been exceeded. Please use an application specific account. Do not use the demo account for your application.","value":18}}

这显然与您期望的不符......