使用RestKit 0.20的对象映射关系

时间:2013-01-29 12:54:11

标签: ios json restkit

我正在尝试使用我的iOS应用中的RestKit 0.20从服务器读取数据并收到以下错误:

//Error

`… I restkit:RKLog.m:34 RestKit logging initialized...`
`… I restkit.support:RKMIMETypeSerialization.m:115 JSON Serialization class 'RKNSJSONSerialization' detected: Registering for MIME Type 'application/json`
`... T restkit.network:RKHTTPRequestOperation.m:139 GET '(null)':
request.headers={
    Accept = "application/json";
    "Accept-Language" = "en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8";
    "User-Agent" = "XXXXX/1.0 (iPhone Simulator; iOS 5.0; Scale/1.00)";
}
request.body=(null)`

`... E restkit.network:RKHTTPRequestOperation.m:150 GET '(null)' (0):
error=Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x6c9d570 {NSUnderlyingError=0x6c9d460 "bad URL", NSLocalizedDescription=bad URL}
response.body=(null)`
`... E restkit.network:RKObjectRequestOperation.m:270 Object request failed: Underlying HTTP request operation failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x6c9d570 {NSUnderlyingError=0x6c9d460 "bad URL", NSLocalizedDescription=bad URL}`

`... Hit error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x6c9d570 {NSUnderlyingError=0x6c9d460 "bad URL", NSLocalizedDescription=bad URL}`

以下是从服务器收到的JSON:

[ {
    "array1" : [ {
    "a1prop1" : 1,
    "a1prop2" : "prop21val",
   }, {
     "a1prop1" : 2,
     "a1prop2" : "prop22val",
    }, {
     "a1prop1" : 3,
     "a1prop2" : "prop23val",
    } ],
  "property1" : prop1val,
  "property2" : "prop2val",
  "property3" : "prop3val",
} ]

使用RestKit进行映射:

-(void) getData {
………………...
NSURL *baseURL = [NSURL URLWithString:stringURL];

AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];

RKObjectMapping *array1Mapping = [RKObjectMapping mappingForClass:[ArrayOneClass class]];
[array1Mapping addAttributeMappingsFromDictionary:@{
 @"a1Prop1" : @"a1Prop1",
 @"a1Prop2" : @"a1Prop2",
 }];

RKObjectMapping *mainObjectMapping = [RKObjectMapping mappingForClass:[MainObjectClass class]]
[mainObjectMapping addAttributeMappingsFromDictionary:@{
 @"property1" : @"property1",
 @"property2" : @"property2",
 @"property3" : @"property3",
 }];

RKRelationshipMapping *mainObjRelationMapping = [RKRelationshipMapping relationshipMappingFromKeyPath:@"array1" toKeyPath:@"array1" withMapping:array1Mapping];
[mainObjectMapping addRelationshipMappingWithSourceKeyPath:@"array1" mapping: mainObjRelationMapping.mapping];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mainObjectMapping pathPattern:nil
                                                                                       keyPath:nil
                                                                                   statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];

NSString *pathStr = [NSString stringWithFormat:@"/api/getAllObjects"];

[objectManager getObjectsAtPath:pathStr
                     parameters:nil
                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                            NSLog(@"...success");
                        }
                        failure:^(RKObjectRequestOperation *operation, NSError *error) {
                            NSLog(@"Hit error: %@", error);
                        }];

}

MainObject类:

// MainObjectClass.h

    @interface MainObjectClass : NSObject{
     int property1;
     NSString * property2;
     NSString * property3;
     NSSet *array1;
    }

@property (nonatomic) int property1;
@property (nonatomic, retain) NSString * property2;
@property (nonatomic, retain) NSString * property3;
@property (nonatomic, retain) NSSet *array1;

- (NSDictionary*)elementToPropertyMappings;
- (NSDictionary*)elementToRelationshipMappings;
@end

// MainObjectClass.m

@implementation MainObjectClass

@synthesize property1, property2, property3, array1;
- (NSDictionary*)elementToPropertyMappings {
    return [NSDictionary dictionaryWithObjectsAndKeys:
        @"property1", @"property1",
        @"property2", @"property2",
        @"property3", @"property3",
        nil];
}
- (NSDictionary *)elementToRelationshipMappings {
    return [NSDictionary dictionaryWithObjectsAndKeys:
        @"array1", @"array1",
        nil];
}
@end

任何人都可以告诉我为什么这没有产生正确的要求?它根本没有击中服务器。我尝试了简单的get / post方法,没有关系映射,它们都可以正常使用RestKit映射。感谢您的观点。谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个: - 使用RKObjectManager

的属性
objectManager.serializationMIMEType = RKMIMETypeJSON;