zoukankan      html  css  js  c++  java
  • Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable con

    转载请注明出处:http://blog.csdn.net/dengbin9009/article/details/43485617

    在使用AFNetworking 2.0  的时候本来一切很顺畅,但是中途遇到几个比较坑的地方

    这里分享一下爬坑经历,忘读者不能速爬坑!

    在发送请求后,NSURLSessionDataTask一直报错

    [html] view plain copy
    1. Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"  

    经过一番网上排查,网上有人说是AF2.0本身的问题,解析格式不全,所以需要在AF的源文件AFURLResponseSerialization.m中修改代码就能解决:

    修改文件223行处

    [objc] view plain copy
    1. self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil nil];  

    为:
    [objc] view plain copy
    1. self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil nil];  

    即可!笔者试过确实可以,但是AF还在持续更新的类库,不宜随意修改,特别是在用了CocoaPods之后,如果之后更新库,此类错误又会重复出现,随后笔者发现acceptableContentTypes是一个开放的属性,既然这样,就证明acceptableContentTypes可以在外部被修改,所以可以在

    初始化HttpClient单利的时候改变这一值:

    [objc] view plain copy
    1. - (instancetype)initWithBaseURL:(NSURL *)url {  
    2.     if (self = [super initWithBaseURL:url]) {  
    3.           
    4.         self.responseSerializer = [AFJSONResponseSerializer serializer];  
    5.         self.requestSerializer.timeoutInterval = TimeoutInterval;  
    6.         self.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil nil];  
    7.         [self setHTTPHeader];  // 可在此处设置Http头信息  
    8.     }  
    9.     return self;  
    10. }  
  • 相关阅读:
    iOS block的用法
    ios-AutoLayout(自动布局代码控制)简单总结
    iOS动画浅汇
    AutoLayout的那些事儿
    ffmpeg合并多个视频
    Win7下安装配置Java
    Linux + Apache + PHP 环境搭建
    Python操作excel文件
    Python文件打包成EXE文件
    Vim插件管理 -- Vundle
  • 原文地址:https://www.cnblogs.com/yecong/p/6141924.html
Copyright © 2011-2022 走看看