• 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. }  
  • 相关阅读:
    Git 数据是怎么存储的
    技术管理规划-路径跟资源
    技术管理规划-如何规划团队的架构
    技术管理规划-如何设定团队的目标
    技术管理规划-设定团队的职能
    springboot实践1
    spring的事件机制实战
    Apollo的基本概念和集成实战
    spring的事件
    ELK的简单安装使用
  • 原文地址:https://www.cnblogs.com/yecong/p/6141924.html
Copyright © 2020-2023  润新知