获取操作时出现 REstkit 错误

Posted

技术标签:

【中文标题】获取操作时出现 REstkit 错误【英文标题】:REstkit Error on Get operation 【发布时间】:2013-10-25 07:13:07 【问题描述】:

我正在执行 Restkit GET 操作,但使用 Restkit 时出现错误。我用 Chrome 上的 REST COnsole 尝试了相同的操作,它可以工作:

GET 操作的详细信息是: 网址:https://www.ez-point.com/ezpoints 操作:获取 授权头:xxxxxxxxxxxxxxxxxxxxx

使用 Chrome REST 控制台,它可以工作,并且我得到了 JSON 格式的正确响应。

使用 Restkit,它不起作用。这些是我的 Restkit 代码:

//trying restkit
    NSURL *endpoint = [NSURL URLWithString:@"https://www.ez-point.com/"];
    RKObjectManager* objectManager = [RKObjectManager managerWithBaseURL:endpoint];
    [objectManager.HTTPClient setAuthorizationHeaderWithToken:@"xxxxxxxxxxxxxxxxxxx"];
    [objectManager getObjectsAtPath:@"ezpoints" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) 
        NSLog(@"Success");
       //NSLog(mappingResult);
     failure:^(RKObjectRequestOperation *operation, NSError *error) 
        NSLog(@"Failure");
        //NSLog(operation);
        //NSLog(error);
    ];

我收到此错误:

2013-10-25 11:00:11.690 EZ-POINT[1089:c07] I restkit:RKLog.m:34 RestKit logging initialized...
2013-10-25 11:00:12.820 EZ-POINT[1089:c07] I restkit.network:RKObjectRequestOperation.m:180 GET 'https://www.ez-point.com/ezpoints'
2013-10-25 11:00:14.184 EZ-POINT[1089:4507] E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/html" UserInfo=0x110a3560 AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://www.ez-point.com/ezpoints>, NSErrorFailingURLKey=https://www.ez-point.com/ezpoints, NSLocalizedDescription=Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x110b42b0>
2013-10-25 11:00:14.185 EZ-POINT[1089:4507] E restkit.network:RKObjectRequestOperation.m:243 GET 'https://www.ez-point.com/ezpoints' (401 Unauthorized / 0 objects) [request=0.0000s mapping=0.0000s total=1.3657s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/html" UserInfo=0x110a3560 AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://www.ez-point.com/ezpoints>, NSErrorFailingURLKey=https://www.ez-point.com/ezpoints, NSLocalizedDescription=Expected content type (
    "application/x-www-form-urlencoded",
    "application/json"
), got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x110b42b0>
2013-10-25 11:00:14.599 EZ-POINT[1089:c07] Failure

有什么建议吗?

【问题讨论】:

【参考方案1】:

错误已经给出了提示:默认情况下它不接受“text/xml”作为内容类型。现在,它接受

application/x-www-form-urlencoded 应用程序/json

您可以使用RKMIMETypeSerialization 注册它并将“text/xml”添加到接受的内容类型之一:

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:RKMIMETypeTextXML];
[objectManager setAcceptHeaderWithMIMEType:@"text/xml"];

【讨论】:

是的,因为您没有注册序列化类(请参阅编辑后的答案)。 使用您的代码,我收到一个错误:未知接收器 'RKXMLReaderSerialization';你是说 RKURLEncodedSerialization 吗? 否 (github.com/RestKit/RKXMLReaderSerialization)。但是:我再次阅读了您的问题,似乎服务器返回了错误的内容类型(文本/xml),因为您实际上正在获取 JSON。因此,只需为此 mime 类型注册 json 序列化或调整您的服务器设置(这是首选方式)。 你说得对,我现在才注意到这一点,对不起,谢谢你的帮助!! 服务器应该知道返回 JSON 或者你应该设置 Accept 标头。

以上是关于获取操作时出现 REstkit 错误的主要内容,如果未能解决你的问题,请参考以下文章

RestKit 链接器错误

RestKit - Lipo 错误:无法在 Xcode5 中打开输入文件

RestKit 更新对象映射空属性

从 Restkit 获取错误描述

Core Data 和 Restkit 简单轻量级迁移错误

RestKit + CoreData:操作成功时替换实体内容,失败时保持原样