json响应被解释为text / plain

时间:2013-01-17 16:30:38

标签: restkit

我有以下代码:

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格式时,为什么会出现此错误?

2 个答案:

答案 0 :(得分:20)

我们做到了。刚设置

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

如果您使用的是JSON而不是XML(我们的情况是XML),请随意使用RKNSJSONSerialization类更改RKXMLReaderSerialization类。

答案 1 :(得分:1)

您没有在响应中正确设置mime类型标头。请注意,错误为got text/plain,而代码期待application/jsonapplication/x-www-form-urlencoded