我正在用restkit 0.2敲打我的头并发布对象。我试图发布一个带有映射的requestDescriptor的对象,但总是在我的请求中得到一个request.body =(null)。我目前完全陷入困境。
我使用以下代码进行映射
// Construct a request mapping for our class
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{
@"command": @"requestedCommand" ,
@"payload": @"payloadForCommand"
}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[ShoppingListAPI class] rootKeyPath:nil];
[manager addRequestDescriptor:requestDescriptor];
// create the object
ShoppingListAPI *shoppingListRequest = [[ShoppingListAPI alloc] init];
shoppingListRequest.payload = payload;
shoppingListRequest.command = @"getShoppingLists";
并尝试使用
发布[manager postObject:shoppingListRequest
path:@"shoppinglistWebservice.html"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
DLog(@"We object mapped the response with the following result: %@", result);
}
failure:^(RKObjectRequestOperation *operation, NSError *error) {
DLog(@"Hit error: %@", error);
}];
从日志中我得到以下
T restkit.network:RKHTTPRequestOperation.m:150 POST 'http://www.myhost.com/shoppinglistWebservice.html':
request.headers={
Accept = "application/json";
"Accept-Language" = "de, en, fr, 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";
"Content-Type" = "text/html; charset=utf-8";
"User-Agent" = "myapp/1.0 (iPhone Simulator; iOS 6.0; Scale/2.00)";
}
request.body=(null)
我遵循RKObjectManager.h的示例实现但没有成功。 request.body保持为null。是不是应该将我的对象显示为JSON字符串?映射工作正常,即
Mapped attribute value from keyPath 'command' to 'requestedCommand'. Value: getShoppingLists
有什么想法吗?
答案 0 :(得分:1)
RKRequestDescriptor *reqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:objectClass rootKeyPath:path];
[objectManager addRequestDescriptor:reqDescriptor];
用于映射使用[mapping inverseMapping]
对于objectClass [Someclass class]
。路径可以是零。
答案 1 :(得分:1)
我不知道你是否设法解决了这个问题,但我终于弄明白为什么我无法发帖 - 这非常令人沮丧:
我需要补充一下:
objectManager.requestSerializationMIMEType=RKMIMETypeJSON;
获取以JSON发布的请求。
从那时起问题就解决了。 无法解决为什么这不是一个更广泛的答案