ios – AFNetworking 2.0和响应序列化选项

前端之家收集整理的这篇文章主要介绍了ios – AFNetworking 2.0和响应序列化选项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_1@一旦我触发对 JSON资源的请求,我就会得到以下信息:
  1. The operation couldnt 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如下:

  1. [{"StoryId":1,"Title":"The big red dog","Abstract":"There was a big red dog and the dog was very big","IsFeatured":true}]
  2.  
  3. -(instancetype) initWithBaseURL:(NSURL *)url
  4. {
  5. NSURL *base = [NSURL URLWithString:@"URL to the web service that returns the json"];
  6. self = [super initWithBaseURL:base];
  7.  
  8. AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
  9.  
  10. [self setResponseSerializer:responseSerializer];
  11.  
  12. return self;
  13. }

返回的错误如下:

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

来自WEB服务器的响应:

  1. [{"StoryId":1,"IsFeatured":true}]

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

  1. HTTP/1.1 200 OK
  2. Server: Microsoft-IIS/7.0
  3. X-Powered-By: ASP.NET
  4. Content-Type: application/json;charset=utf-8
  5. X-AspNetMvc-Version: 3.0
  6. Connection: close
  7. Date: Mon,28 Oct 2013 20:02:21 GMT
  8. Content-Length: 121
  9. Cache-Control: private
  10. X-AspNet-Version: 4.0.30319
  11.  
  12. [{"StoryId":1,"IsFeatured":true}]

解决方法

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

猜你在找的iOS相关文章