我從服務對象的兩個不同的反應:如何在RestKit中映射動態響應?
這是第一個響應的時候沒有錯誤:
{"responseError":{"ApplicationErrorCode":101,"DeveloperMessage":"Code is needed","UserMessage":null},"shortToken":null}
這是第二個響應時,有一個錯誤:
{"responseError":null,"shortToken":sometoken}
我已經創建了一個名爲RKShortToken
一個對象,它看起來像這樣:
@interface RKShortToken : NSObject
@property (copy, nonatomic) RKResponseError *responseError;
@property (copy, nonatomic) NSString *shortToken;
@end
而且我有這個對象調用RKResponseError
:
@interface RKResponseError : NSObject
@property (copy, nonatomic) NSString *applicationErrorCode;
@property (copy, nonatomic) NSString *developerMessage;
@property (copy, nonatomic) NSString *userMessage;
@end
首先初始化RestKit客戶端:
NSURL *baseURL = [NSURL URLWithString:kServiceDevBaseURL];
self.manager = [RKObjectManager managerWithBaseURL:baseURL];
[self.manager setRequestSerializationMIMEType:RKMIMETypeJSON];
[self.manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];
[self.manager addResponseDescriptorsFromArray:[RKObjectManager sharedManager].responseDescriptors];
[self.manager addRequestDescriptorsFromArray:[RKObjectManager sharedManager].requestDescriptors];
[self.manager.HTTPClient.operationQueue setMaxConcurrentOperationCount:5];
[RKObjectManager setSharedManager:self.manager];
比添加路線:
RKRoute *shortTokenRoute = [RKRoute routeWithClass:[RKShortToken class] pathPattern:@"users/login/quick/shortToken" method:RKRequestMethodGET];
shortTokenRoute.shouldEscapePath = YES;
[self.manager.router.routeSet addRoute:shortTokenRoute];
而不是添加一個描述:
RKObjectMapping *shortTokenMapping = [RKObjectMapping mappingForClass:[RKShortToken class]];
[shortTokenMapping addAttributeMappingsFromDictionary:@{ @"responseError" : @"responseError", @"shortToken" : @"shortToken" }];
RKResponseDescriptor *shortTokenDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:shortTokenMapping
method:RKRequestMethodAny
pathPattern:@"users/login/quick/shortToken"
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[RKObjectManager sharedManager] addResponseDescriptor:shortTokenDescriptor];
如何處理這種反應與RestKit的?我認爲我的路線或描述符是錯誤的..
在此先感謝!
你的例子是錯誤的嗎?這是如何動態的?你有什麼嘗試? – Wain
@爲什麼我編輯了我的問題。請檢查出來。 –