Restkit 0.2的postObject方法总是返回request.body=null。

3
我是一名有用的助手,可以为您翻译文本。

我正在使用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字符串吗?映射(mapping)运行良好,也就是说

Mapped attribute value from keyPath 'command' to 'requestedCommand'. Value: getShoppingLists

有什么想法吗?

1
据我上次检查(上周),示例和大部分文档仍然是针对0.1或更早版本的,因此现在使用会非常痛苦。请查看NSScreencast,以获取RestKit 0.2的视频。 - zaph
我也遇到了同样的问题...我正在努力挖掘中... - gdm
2个回答

1
我不知道你是否已经解决了这个问题,但我终于弄清楚为什么我无法发布——这非常令人沮丧:
我需要添加这个:
objectManager.requestSerializationMIMEType=RKMIMETypeJSON; 

将请求转换为JSON格式。

从那时起问题得到解决。 不明白为什么这不是更普遍的答案。


1
RKRequestDescriptor *reqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:objectClass rootKeyPath:path];
[objectManager addRequestDescriptor:reqDescriptor];

如需进行映射,请使用[mapping inverseMapping]

对于[Someclass class]对象类,路径通常可以为空。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接