【问题标题】:Restkit 0.2 postObject always returns request.body=(null)Restkit 0.2 postObject 总是返回 request.body=(null)
【发布时间】:2013-02-24 16:28:03
【问题描述】:

我正在用 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 保持为空。它不应该将我的对象显示为 JSON 字符串吗?映射工作正常,即

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

有什么想法吗?

【问题讨论】:

  • 我最后一次检查(上周)示例和大多数文档仍然是 0.1 或更早版本,因此现在是一个痛苦的世界。查看 NSScreencast 获取 RestKit 0.2 的视频。
  • 同样的问题......我正在努力挖掘......

标签: ios restkit


【解决方案1】:
RKRequestDescriptor *reqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:mapping objectClass:objectClass rootKeyPath:path];
[objectManager addRequestDescriptor:reqDescriptor];

映射使用[mapping inverseMapping]

对于对象类[Someclass class]。 Path 大多可以为 nil。

【讨论】:

    【解决方案2】:

    我不知道你是否设法解决了这个问题,但我终于弄清楚了为什么我无法发帖 - 这非常令人沮丧:

    我需要添加这个:

    objectManager.requestSerializationMIMEType=RKMIMETypeJSON; 
    

    获取以 JSON 格式发布的请求。

    问题从此解决。 无法弄清楚为什么这不是一个更广泛的答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      • 2016-11-02
      • 2014-05-06
      • 2012-06-05
      相关资源
      最近更新 更多