iOS开发 AFNetworking 关于JSON text did not start with array or object and option to allow fragments not
来源:互联网 发布:linux打印行号 编辑:程序博客网 时间:2024/05/22 10:47
AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; [manager GET:@"http://www.baidu.com" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@",result); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"%@",error); }];
在AFNetworking中 上述代码将会 输出
Error Domain=NSCocoaErrorDomain Code=3840 "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=0x8ea19a0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
AFNetworking默认把响应结果当成json来处理,(默认manager.responseSerializer = [AFJSONResponseSerializer serializer]) ,很显然,我们请求的百度首页 返回的并不是一个json文本,而是一个html网页,但是AFNetworking并不知道,它坚信请求的结果就是一个json文本!然后固执地以json的形式去解析,显然没办法把一个网页解析成一个字典或者数组,所以产生了上述错误.
然而,我们期望它能够正确地处理这个情形,而不是提示一个错误.
这时候 你必须告诉AFNetworking:别把这个网页当json来处理!
只需要在发送请求前加入:manager.responseSerializer = [AFHTTPResponseSerializer serializer];
代码变成:
AFHTTPSessionManager *manager =[AFHTTPSessionManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; [manager GET:@"http://www.baidu.com" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) { NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding]; NSLog(@"%@",result); } failure:^(NSURLSessionDataTask *task, NSError *error) { NSLog(@"%@",error); }];
这样,就可以正确地把baidu.com首页的html源码抓回来啦.
如果上面不是上面这种情况:就有可能是后台接口问题,可以和后要交流一下,询问下情况。我本人就遇到过到这种情况,和后台交流后发现是后台接口问题,导致无法正常返回json数据(后台是php写的)。
1 0
- iOS开发 AFNetworking 关于JSON text did not start with array or object and option to allow fragments not
- AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set
- AFNetworking 报错 JSON text did not start with array or object and option to allow fragments not set
- Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option t
- Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option
- Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option t
- error:Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and op
- iOS开发Post请求错误:Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or ...
- Table configuration with catalog null, schema orcl, and table ORDERS did not resolve to any tables
- The service did not start due to a logon failure
- IIS无法启动,报错:The service did not respond to the start or control request in a timely fashion.
- How ECMAScript 5 still does not allow to subclass array
- 关于: No Hibernate Session bound to thread, and configuration does not allow creation of non-transacti
- XML or text declaration not at start of entity
- want to write, and always did not think
- Timeout waiting for JBOSS 4.2 to start. Server did not start after 50s
- javascript: Jquery each loop with json array or object
- javascript: Jquery each loop with json array or object
- Android 四大组件之广播 -- 红
- 第11周项目1-验证算法(4)哈夫曼编码的算法验证
- Servlet学习之一开发Servlet的三种方法
- 理解临时对象的来源
- notepad++保存后中文乱码的问题
- iOS开发 AFNetworking 关于JSON text did not start with array or object and option to allow fragments not
- (第十二周项目3)图遍历算法实现
- ARecyclerView,一个库搞定recyclerview
- Android Service后台进程守护
- opencv3.1.0+cuda8.0+vs2013+win7编译过程错误及解决方法
- UIImagePickerController status Bar can not hidden in IOS 10
- 电商Hadoop项目第一天
- 十二周项目四--利用遍历思想求解图问题(6-7)
- 第十二周项目5 - 迷宫问题之图深度优先遍历解法