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];

「ニュース」は私が使用しているクラスであり、使用しているキーパス「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として表示されていますが、ブラウザでは上記の応答を取得しています。

助けてください

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 に答える