2

我正在研究 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“地名”

@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 但是在浏览器中,我得到了上面提到的响应。

请帮忙

4

1 回答 1

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}}

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

于 2013-05-30T06:55:14.100 回答