使用 RestKit 0.20 的对象映射关系
Posted
技术标签:
【中文标题】使用 RestKit 0.20 的对象映射关系【英文标题】:Object Mappings Relationship using RestKit 0.20 【发布时间】:2013-01-29 12:54:11 【问题描述】:我尝试在我的 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 映射。欣赏您的观点。谢谢。
【问题讨论】:
错误的 URL,表示您发布到的 URL 有问题。您能否在问题中添加一些有关您正在使用的 URL 的其他详细信息? 【参考方案1】:试试这个:- 使用RKObjectManager
的属性
objectManager.serializationMIMEType = RKMIMETypeJSON;
【讨论】:
以上是关于使用 RestKit 0.20 的对象映射关系的主要内容,如果未能解决你的问题,请参考以下文章