AFNetworking 2.0和响应序列化选项

时间:2013-10-28 19:36:21

标签: ios afnetworking afnetworking-2

一旦我触发对JSON资源的请求,我就会得到以下内容:

The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x1d87a2c0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

问题是我应该在哪里设置AFNetworking 2.0中的允许片段?

更新:

我的类是AFHTTPSessionManager的子类。我已经创建了一个JSONResponseSerializer,它也不起作用。

返回的JSON如下:

   [{"StoryId":1,"Title":"The big red dog","Abstract":"There was a big red dog and the dog was very big","IsFeatured":true}]

-(instancetype) initWithBaseURL:(NSURL *)url
{
    NSURL *base = [NSURL URLWithString:@"URL to the web service that returns the json"];
    self = [super initWithBaseURL:base];

    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

    [self setResponseSerializer:responseSerializer];

    return self;
}

返回的错误如下:

[0] (null)  @"NSDebugDescription" : @"Invalid value around character 0."

从WEB服务器响应:

[{"StoryId":1,"Title":"The big red dog","Abstract":"There was a big red dog and the dog was very big","IsFeatured":true}]

来自WEB服务器的响应状态:

HTTP/1.1 200 OK
Server: Microsoft-IIS/7.0
X-Powered-By: ASP.NET
Content-Type: application/json;charset=utf-8
X-AspNetMvc-Version: 3.0
Connection: close
Date: Mon, 28 Oct 2013 20:02:21 GMT
Content-Length: 121
Cache-Control: private
X-AspNet-Version: 4.0.30319

[{"StoryId":1,"Title":"The big red dog","Abstract":"There was a big red dog and the dog was very big","IsFeatured":true}]

2 个答案:

答案 0 :(得分:7)

您需要使用AFJSONResponseSerializer创建自己的serializerWithReadingOptions:实例,并将系统配置为使用它(代替默认的JSON响应序列化程序)。

答案 1 :(得分:0)

之前我遇到了同样的问题。经过几个小时的搜索和阅读,我找到了一些东西:

  

self.responseSerializer = [AFHTTPResponseSerializer serializer];           self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@" application / json; charset = UTF-8",nil];

试试吧,祝你好运!