首先我们来看一下错误信息:

Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 406"

{ status code: 406,headers {

"Content-Language" = en;
"Content-Length" = 1110;
"Content-Type" = "text/html;charset=utf-8";
Date = "Sat, 27 Sep 2014 05:29:13 GMT";
Server = "Apache-Coyote/1.1";
} }

相信很多小伙伴会遇到这种问题,又找不到方法解决.然后今天的项目再次出现了这个问题,因为之前遇到过类似问题,是通过搜索"text/"找到下面这段代码:

+ (NSSet *)acceptableContentTypes

{
  return [NSSet setWithObjects:@"text/html", @"text/plain", @"application/json", @"text/json", @"text/javascript", nil];
}

在中间插入@"text/html",基本上问题就解决了,但是这次却没有.我来来回回看了好多变代码,也测试了好几次,最后发现了问题,原来是我在封装请求方法时没有加入请求头协议:

[httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[httpClient setDefaultHeader:@"Accept" value:@"application/json"];

加上上面俩句后问题马上得到解决.

最后总结一下:一般遇到这种情况,先查看

+ (NSSet *)acceptableContentTypes

{
  return [NSSet setWithObjects:@"text/html", @"text/plain", @"application/json", @"text/json", @"text/javascript", nil];
}

这个方法中有没有包含服务器返回的数据格式,如果没有就加上.然后执行代码测试是否通过,如果未通过,再看一下你封装的请求方法中是否没有加入请求头协议.下面是我的完整的封装GET和POST请求方法代码,给大家参考一下:

+ (void)postWithBaseURL:(NSString *)baseURL path:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure
{
// 封装请求
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:baseURL]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
NSURLRequest *post = [client requestWithMethod:@"POST" path:path parameters:params];

// 创建AFJSONRequestOperation对象
NSOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:post success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
success(JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
failure(error);
}];

// 发送请求
[operation start];
}

+(void)getWithBaseURL:(NSString *)baseURL path:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure
{
// 封装请求
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:baseURL]];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];
NSURLRequest *post = [client requestWithMethod:@"GET" path:path parameters:params];

// 创建AFJSONRequestOperation对象
NSOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:post success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
success(JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
failure(error);
}];

// 发送请求
[operation start];
}

博客写的较仓促,所以代码没有封装的很细,这里仅供大家参考.

AFNetworkingErrorDomain 错误解决方法的更多相关文章

  1. SQL SERVER 9003错误解决方法 只适用于SQL2000

    SQLSERVER 9003错误解决方法 只适用于SQL2000 (只适用于SQL2000) "无法打开新数据库 'POS'.CREATE DATABASE 中止. (Microsoft S ...

  2. New XAMPP security concept:错误解决方法

    New XAMPP security concept:错误解决方法 (2014-03-06 16:07:46) 转载▼   分类: php 在Linux上配置xampp后远程访问域名报错: New X ...

  3. hadoop本地库与系统版本不一致引起的错误解决方法

    hadoop本地库与系统版本不一致引起的错误解决方法 部署hadoop的集群环境为 操作系统 centos 5.8 hadoop版本为cloudera   hadoop-0.20.2-cdh3u3 集 ...

  4. mysql Access denied for user root@localhost错误解决方法总结(转)

    mysql Access denied for user root@localhost错误解决方法总结(转) mysql Access denied for user \'root\'@\'local ...

  5. Eclipse 出现Some sites could not be found. See the error log for more detail.错误 解决方法

    Eclipse 出现Some sites could not be found.  See the error log for more detail.错误 解决方法 Some sites could ...

  6. eWebeditor编辑器上传图片路径错误解决方法[疑难杂症]【转,作者:unvs】

    做了一个多版本的网站,后台用的编辑器是eWebeditor,NET版,后面发现上传图片或者文件之后,路径错误无法显示,必须手工修改才行.. 为了更清楚的说明问题,我下面会说的比较详细,首先是网站文件框 ...

  7. 网站启动SSL, http变为https后,session验证码错误解决方法

    网站启动SSL, http变为https后,session验证码错误解决方法   最近公司需要后台启动安全证书,证书安装完毕后,后台老提示 验证码错误,经过几天的研究,此问题已经得到有效解决,现把方法 ...

  8. Oracle OCI-22053:溢出错误解决方法

    原文 Oracle OCI-22053:溢出错误解决方法 Oracle 数值数据类型最多可存储 38 个字节的精度.当将 Oracle 数值转换为公共语言运行库数据类型时,小数点后边的位数可能过多,这 ...

  9. Nginx出现“413 Request Entity Too Large”错误解决方法

    Nginx出现“413 Request Entity Too Large”错误解决方法 2011-03-25 13:49:55|  分类: 默认分类 |  标签:413  request  entit ...

随机推荐

  1. 弹出的ViewController半透明效果

    在第一个ViewController做如下设置 let controller = UIStoryboard(name: "Main", bundle: nil).instantia ...

  2. matlab mod()&rem()

    一.介绍rem/mod(X,Y)当X,Y符号相同的时候,这两者一样,当符号不相同的时候才有差别,具体表现在rem结果的符号与被除数相同:mod结果的符号与除数相同 二.例子 >> a=9; ...

  3. Oracle Statistic 统计信息 小结

    oraclestatisticstabledatabasesqldictionary   目录(?)[-] 直方图上列的信息说明 直方图类型说明   一.  Statistic 说明 Oracle 官 ...

  4. 前端jquery---表单验证

    重点: 1.表单的提交 2.触发blur事件 3.判断是否正确,提交与否 return False <!DOCTYPE html> <html lang="en" ...

  5. CTF-练习平台-Misc之 听首音乐

    十九.听首音乐 用软件audacity打开后发现如下 上面那一行是可以看做摩斯电码,翻译过来是: ..... -... -.-. ----. ..--- ..... -.... ....- ----. ...

  6. ES6必知必会 (三)—— 数组和对象的拓展

    数组的扩展 1.拓展运算符('...'),它相当于rest参数的逆运算,用于将一个数组转换为用逗号分隔的参数序列: console.log(...[1, 2, 3]) // 1 2 3 console ...

  7. read()/fread()/mmap()执行效率对比

    一. read()/fread()/mmap()执行效率对比 系统调用read.c: #include <sys/types.h> #include <sys/stat.h> ...

  8. MySQL Disk--SSD与RAID

    ===================================================SSD与RAID 51.在RAID 5这类Parity-RAID上存在partial-stripe ...

  9. GridControl 之 BandedGridView

    https://documentation.devexpress.com/#WindowsForms/clsDevExpressXtraGridViewsBandedGridBandedGridVie ...

  10. 序列化效率比拼——谁是最后的赢家avaScriptSerializer方式、DataContract方式、Newtonsoft.Json

    前言:作为开发人员,对象的序列化恐怕难以避免.楼主也是很早以前就接触过序列化,可是理解都不太深刻,对于用哪种方式去做序列化更是随波逐流——项目中原来用的什么方式照着用就好了.可是这么多年自己对于这东西 ...