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源码抓回来啦.

AFNetworking 关于JSON text did not start with array or object and option to allow fragments not set 错误的更多相关文章

  1. AFNetworking 不支持 text/plain,unacceptable content-type: text/plain

    1. 用AFNetworkingPOST传递参数(获取微博的accessToken)的时候,具体代码如下: AFHTTPSessionManager *session = [AFHTTPSession ...

  2. solr File Upload "Unsupported ContentType: application/vnd.ms-excel Not in: [application/xml, application/csv, application/json, text/json, text/csv, text/xml, application/javabin]",

    今天在用solr管理界面导入文件时报错:"Unsupported ContentType: application/vnd.ms-excel  Not in: [application/xm ...

  3. json解析bug之ERROR ExceptionController:185 - not close json text, token : :

    错误:ERROR ExceptionController:185 - not close json text, token : : 原因:json数据格式有误.!我的错误是,缺少了一个包括json数据 ...

  4. [Javascript] Classify JSON text data with machine learning in Natural

    In this lesson, we will learn how to train a Naive Bayes classifier and a Logistic Regression classi ...

  5. Spring MVC 解决 Could not write JSON: No serializer found for class java.lang.Object

    Spring MVC 解决 Could not write JSON: No serializer found for class java.lang.Object 资料参考:http://stack ...

  6. Poco::JSON::Array 中object 设置preserveInsertionOrder 时,stringify出错-->深入解析

    在使用poco version 1.6.0时 Poco::JSON::Array 在object  设置preserveInsertionOrder =true 时 调用 array.stringif ...

  7. json解析json字符串时候,数组必须对应jsonObjectArray,不能对应JsonObject。否则会解析错误。

    json第三方解析json字符串时候,json数组必须对应jsonObjectArray,不能对应JsonObject.->只要是[]开头的都是json数组字符串,就要用jsonArray解析 ...

  8. (三十六)利用AFNetworking进行JSON数据解析

    1.首先要安装CocoaPods,然后在需要AFNetworking的工程根目录建立podfile,内容如下: platform :ios, '7.0' pod 'AFNetworking' 2.然后 ...

  9. 使用AFNetWorking读取JSON出现NSCocoaErrorDomain Code=3840的解决方法

    最近在使用AFNetworkWorking读取JSON时,出现了NSCocoaErrorDomain Code=3840的错误.这种错误应该是服务器端返回JSON格式不对造成的.通过Google搜到这 ...

随机推荐

  1. AJAX应用的五个步骤

    1.建立xmlHttpRequest对象 if(window.XMLHttpRequest) {    xmlHttp = new XMLHttpRequest();    if(xmlHttp.ov ...

  2. XTU 1246 Heartstone

    $2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$D$题 贪心. 我是这样贪的:开三个优先队列$q[0]$,$q[1]$,$q[2]$,$q[i]$存储对$3$取余之后为$i$的数. 首先看看还 ...

  3. CodeForces 706C Hard problem

    简单$dp$. $dp[i][0]$:第$i$个串放置完毕,并且第$i$个串不反转,前$i$个串字典序呈非递减的状态下的最小费用. $dp[i][1]$:第$i$个串放置完毕,并且第$i$个串反转,前 ...

  4. jquery.ajax异步发送请求的简单测试

    使用ajax异步发送请求到一般处理程序,判断输入的用户名和密码 1.添加Html页面,导入jquery 2.编写js代码和页面标签 <script type="text/javascr ...

  5. webapi mvc路由注册

    在VS.NET 2013中,新建WebAPI项目,代码总的 GlobalConfiguration.Configure(WebApiConfig.Register); 编译时会提示:System.We ...

  6. python 之调用Linux shell命令及相关高级应用

    最近根据老大要求,将数据进行同步备份,结合第三方提供的工具.第三方服务其实是有python demo的,本想研究下实际的python sdk搞个demo开发的,但是发现有些组建装起来确实头大,而且本公 ...

  7. 《HTML5与CSS3权威指南》读书笔记(下册)—CSS3篇

    大而全的CSS3 API类型书,并带有一些实用案例讲解,层次分明分类明确,新增技术都做了详情介绍.个人觉得如果在细节和文字表达上再下些功夫会更出色,其中部分内容如:rem.Media Queries网 ...

  8. ng-Directive

    伪代码: var myModule = angular.module(...); myModule.directive('namespaceDirectiveName', function facto ...

  9. Xcode 8 控制台输出大量不用的log的问题解决&&NSLog失效的解决

    Product-->Scheme-->editeScheme中:Auguments中Environment Variable中 Scheme中添加环境变量 "OS_ACTIVIT ...

  10. opencv3.1自带demo的介绍和运行操作。转载

    opencv3.1自带demo的介绍和运行操作. 下列实验基本都试过,有些需要根据自己的电脑修改一些路径或者调试参数. 值得注意的是,控制台程序输入有时候要在图像所在的窗口输入相应的指令.我的电脑上安 ...