Ios AFNetworking 2.0和响应序列化选项
一旦触发对JSON资源的请求,我就会得到以下结果:Ios AFNetworking 2.0和响应序列化选项,ios,afnetworking,afnetworking-2,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 ob
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.}
[0] (null) @"NSDebugDescription" : @"Invalid value around character 0."
问题是,在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;
}
返回的错误如下所示:
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.}
[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}]
您需要使用带有读取选项的
serializer:
创建自己的AFJSONResponseSerializer
实例,并将系统配置为使用它(代替默认的JSON响应serialiser)。我以前遇到过同样的问题。经过几个小时的搜索和阅读,我发现了一些东西:
self.responseSerializer=[AFHTTPResponseSerializer序列化程序];
self.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@“application/json;charset=UTF-8”,nil]
试试看,祝你好运 这是web服务器响应的问题,而不是AFNetworking的问题。响应是有效的!我只是验证了JSON,返回的JSON是有效的,我没有说它无效。我说问题是由它引起的。JSON数据往往包含在数组或对象中@Wain是对的,你必须告诉AFNetworking哪些选项应该
NSJSONSerialization
用于读取JSON数据。我已经这样做了,看看我更新的问题!啊,我看到了你的编辑,我收回了我的话。请看我更新的问题!我添加了AFJSONResponseSerializer,但仍然没有用!您的JSON看起来没有片段。您确定正在使用您的自定义类吗?。。。您确定响应没有损坏或包含在其他内容中吗?请参阅标题为“来自WEB服务器的响应”的问题。这就是我得到的实际反应。