ios - 使用 RestKit 0.20 的对象映射关系

标签 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);
                        }];

}

主对象类:

//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 映射正常工作。感谢您的意见。谢谢。

最佳答案

试试这个:-使用RKObjectManager的属性

objectManager.serializationMIMEType = RKMIMETypeJSON;

关于ios - 使用 RestKit 0.20 的对象映射关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14583393/

相关文章:

ios - 为什么我在左侧边栏的 xcode 中看不到我工作区中的所有文件?

iOS:核心数据和磁盘空间

perl - 为什么我不能通过 Perl 通过 JSON::XS 从 PostgreSQL 正确编码一个 bool 值?

swift - Xcode10.1 RestKit错误_$s7RestKit11DynamicKeysVN

ios - 从 Storyboard 中实例化几个可拖动的图 block - 附上测试代码和屏幕截图

ios - Objective-C 中的开放时间 - 两次 NSDate 之间的时间

java - jackson json 到 Object 解析

jquery - 使用 Ajax 和 Web Api 的级联下拉菜单

ios - RKObjectManager 映射结果问题

ios - RestKit POST 发送 JSON 不包含映射路径/类名