2013-10-29 256 views
0

一個原始JSON響應予有需要張貼一複雜的JSON對象的API。 API保存並響應一個原始ID(或一個錯誤對象)。我無法映射原始ID。有任何想法嗎?下面的例子,爲了簡單起見,是不是做的POST對象映射,但是我的一些API調用都需要這一點。映射與RestKit v0.21

只見建議利用RestKit建立請求,並將它傳遞給AFNetworking,但這不會分析可能的錯誤返回對象。

RKObjectMapping* map = [RKObjectMapping mappingForClass:[MyPrimitiveResponse class]]; 
[map addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"success"]]; 

RKResponseDescriptor *errDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[MyErrorResponse objectMap] method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassServerError)]; 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:map method:RKRequestMethodGET pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 

NSURL *URL = [NSURL URLWithString:apiUrl]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ errDescriptor, 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); 
}]; 

[objectRequestOperation start]; 

我得到的調試器以下錯誤:

2013-10-29 10:23:15.196 TestParser[6988:70b] E app:MyHomeViewController.m:54 Operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'" UserInfo=0x8daca20 {NSErrorFailingURLKey=....., NSUnderlyingError=0x8db4cd0 "The operation couldn’t be completed. (Cocoa error 3840.)", NSLocalizedDescription=Loaded an unprocessable response (200) with content type 'application/json'}

UPDATE:
這是我的代碼來處理這種情況最終位。它可能證明是有益的其他...

// Manually Map Request to JSON & send to server 
NSDictionary *parameters = [RKObjectParameterization parametersWithObject:payload requestDescriptor:[payload.class requestDescriptor] error:&error]; 
NSMutableURLRequest* request = [self.apiManager requestWithObject:nil method:RKRequestMethodPOST path:url parameters:parameters]; 

RKHTTPRequestOperation *requestOperation = [[RKHTTPRequestOperation alloc] initWithRequest:request]; 
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    unichar firstChar = [operation.responseString characterAtIndex:0]; 
    NSData *newData; 
    if (firstChar != '{' && firstChar != '[') { 
     // Force into JSON array so it can be parsed normally 
     newData = [[[@"[" stringByAppendingString:operation.responseString] stringByAppendingString:@"]"] dataUsingEncoding:NSUTF8StringEncoding]; 
    } else { 
     newData = operation.responseData; 
    } 

    // Parse JSON response into native object, whether primitive NSNumber (integer or boolean) response or a full fledged JSON error object. 
    RKResponseDescriptor *errDescriptor = [MyErrorResponse responseDescriptor]; 
    RKObjectResponseMapperOperation* mapper = [[RKObjectResponseMapperOperation alloc] initWithRequest:request response:operation.response data:newData responseDescriptors:@[errDescriptor, [payload.class responseDescriptor]]]; 
    [mapper setDidFinishMappingBlock:^(RKMappingResult *mappingResult, NSError *error) { 
     if (mappingResult) { //Success 
      RKLogInfo(@"Load response: %@", mappingResult.firstObject); 
     } else { 
      RKLogError(@"Operation failed with error: %@", error); 
     } 
    }]; 
    [mapper start]; 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    RKLogError(@"Operation failed with error: %@", error); 
}]; 

[requestOperation start]; 

回答

1

I saw suggestions to utilize RestKit to build the request, and pass it to AFNetworking

是,做到這一點。

but this will not parse a possible error return object

是的,但您可以使用RestKit中的映射操作來處理該操作。或者,如果錯誤的反應是比較簡單的只使用NSJSONSerialization(或類似),以響應轉換。


'原始ID'應該可映射爲nil-keypath映射。但你仍然可以在沒有正在映射它放回在POST請求中使用的源對象映射錯誤響應的問題。

+0

你能解釋你的迴應的最後一部分嗎?應該如何構造源對象以包含響應對象?我是否應該添加一個錯誤元素並映射該鍵而不是在某處? –

+0

我會選擇第一個選項。你可能會遇到第二個問題。 – Wain