2013-01-17 52 views
11

我有以下代码:JSON响应被解释为纯文本/

NSURL *URL = [NSURL URLWithString:[@"some-address"]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL]; 

RKObjectRequestOperation *requestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:[self.objectManager responseDescriptors]]; 

[requestOperation start]; 
[requestOperation waitUntilFinished]; 

我碰到下面的错误。

Object request failed: Underlying HTTP request operation failed with error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "application/x-www-form-urlencoded", 
    "application/json" 
)}, got text/plain" UserInfo=0x1f5e3c40 {NSLocalizedRecoverySuggestion={"total_rows":16,"offset":1,"rows":[ 

{"id":"1","key":1,"value":{"_id":"1","_rev":"1-e75042683867a7030fc4d3aa3b72ef35", 
"user":{ 
"userId":"1", 
"name":"A", 
....... 
]}}, ..... 

为什么我得到这个错误,当响应是JSON格式?

回答

20

我们做到了。刚刚成立

[RKMIMETypeSerialization registerClass:[RKXMLReaderSerialization class] forMIMEType:@"text/plain"]; 

,并随时如果你使用JSON而不是XML的改变RKXMLReaderSerialization类RKNSJSONSerialization类(XML是我们的情况下)。

+0

谢谢帮助 – zyxel

+0

Stefano Mondino,你是男人! –

+0

拯救了我的生命非常感谢你 –

1

你没有在你的反应正确设置的mime-type头。注意错误说got text/plain,而代码期望application/jsonapplication/x-www-form-urlencoded

+0

如果什么MIME类型(服务器端)是不是我的控制之下?我使用Restkit 0.20 –

+0

然后看看你是否可以覆盖/加 –

+0

已经完成客户端可以接受的MIME类型。我用这个: [RKMIMETypeSerialization的registerClass:[RKXMLReaderSerialization类] forMIMEType:@ “text/plain的”]; 但它保持失败的对象映射。每个网络请求/响应在我的所有应用程序中都能正常工作,除此之外我们已经通知“服务人员”关于这个错误(当你实际发送一个真正格式良好的XML时,没有理由设置文本/纯文本类型),但他们不会修复它。 必须有XML解析器里面的东西(这是不是Restkit的,因为0.20了),也许我应该打开另一个话题了这个(自然失业率被问及JSON) –