2013-10-29 211 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