RestKit: 如何处理空的 response.body?

3

我使用POST请求数据,但服务器只是返回状态码200和内容长度为0。我该如何处理?我无法添加不带映射的RKResponseDescriptor,也不能不添加RKResponseDescriptor。


这里有一个非常相似的问题,答案也很类似:http://stackoverflow.com/questions/21830144/ios-and-restkit-how-to-get-a-text-html-response-right/43682106#43682106 我的回答同样适用于当前的问题 :) 如果响应为空,也许你应该直接使用 AFNetworking,因为不需要映射。 - GabLeRoux
1个回答

12

啊,我刚才发现,你可以为NSNull类创建一个映射。这似乎有效。下面是代码:

    {
        RKObjectMapping* responseMapping = [RKObjectMapping mappingForClass:[NSNull class]];
        RKResponseDescriptor* response =
        [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                     method:RKRequestMethodAny
                                                pathPattern:@"entry/sync"
                                                    keyPath:nil
                                                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

        [restObjectManager addResponseDescriptor:response];
    }
请告诉我这是否是正确的方法。

这对我也起作用了,但由于服务器返回的是空的 text/plain 响应,我不得不为此 mimetype 注册一个序列化器,像这样:[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];AppDeletegate.m 中。由于答案为空,所以 RKNSJSONSerialization 完全可以胜任。 - GabLeRoux

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