AFNetworking 3.0 Code=-1016 错误解决方案

简介: AFNetworking 3.0 Code=-1016 错误解决方案导入AFNetworking类库(请参考:iOS 9 导入类库全面详尽过程(Ruby安装->CocoaPods安装->导入类库))之后,然后小伙伴们就可以照着如下官方文档欢乐地写代码。AFHTTPSessionManager *manager = [AFHTTPSessionManage

AFNetworking 3.0 Code=-1016 错误解决方案

导入AFNetworking类库(请参考:iOS 9 导入类库全面详尽过程(Ruby安装->CocoaPods安装->导入类库))之后,然后小伙伴们就可以照着如下官方文档欢乐地写代码。

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://blog.csdn.net/sps900608" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    NSLog(@"Success: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];

或是用Swift:

let sessionManager = AFHTTPSessionManager()
sessionManager.GET("http://blog.csdn.net/sps900608", parameters: nil, progress: nil, success: { (task, responseObject) in
            NSLog("Success:\(responseObject)");
            }) { (task, error) in
            NSLog("Error:\(error)")
        }

结果就妥妥地报错了

Error:Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7fc723e290c0> { URL: http://m.blog.csdn.net/blog/index?username=sps900608 } { status code: 200, headers {
    "Cache-Control" = private;
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "text/html; charset=utf-8";
    Date = "Fri, 20 May 2016 09:01:39 GMT";
    "Keep-Alive" = "timeout=20";
    Server = openresty;
    "Transfer-Encoding" = Identity;
    Vary = "Accept-Encoding";
    "X-AspNetMvc-Version" = "3.0";
    "X-Powered-By" = "PHP 5.4.28";
} }, NSErrorFailingURLKey=http://m.blog.csdn.net/blog/index?username=sps900608

搜索 “Request failed: unacceptable content-type: text/html” 的解决方案,会有很多人告诉你去修改AFURLResponseSerialization类中的

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];

改为:

self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];

或者在AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];之后加一行这样的代码:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];

接下来又有可能报另一个错误

Error:Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}

为了从根本上解决 Code=-1016 错误,并避免 Code=3840 错误,其实只需要一行代码就能搞定:

sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer]

Swift:

sessionManager.responseSerializer = AFHTTPResponseSerializer()

示例代码:

import UIKit
import AFNetworking

class ViewController: UIViewController {

    @IBOutlet var webView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let sessionManager = AFHTTPSessionManager()
        sessionManager.responseSerializer = AFHTTPResponseSerializer()
        sessionManager.GET("http://blog.csdn.net/sps900608", parameters: nil, progress: nil, success: { (task, responseObject) in
            let data = responseObject as! NSData
            self.webView.loadData(data, MIMEType: "text/html", textEncodingName: "utf-8", baseURL: NSURL())
            }) { (task, error) in
                NSLog("Error:\(error)")
        }

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

运行效果:
这里写图片描述

目录
相关文章
|
人工智能 监控 测试技术
利用AI辅助工具提升软件测试效率
【2月更文挑战第17天】 随着科技的不断发展,人工智能(AI)在各个领域的应用越来越广泛。在软件测试领域,AI技术也发挥着重要作用。本文将探讨如何利用AI辅助工具提升软件测试效率,包括自动化测试、智能缺陷识别和预测等方面。通过引入AI技术,软件测试过程将变得更加高效、准确和可靠。
1224 1
|
测试技术 Python Shell
Python Locust对指定网站“一键压测”
[本文出自天外归云的博客园] 前篇 前篇:Python Locust性能测试框架实践 本篇 承上——归纳过程 在前篇的基础上,我们可以利用Locust性能测试框架编写python脚本对指定网站或者接口进行压测,对于Locust的使用我们可以归纳为三步: 1. 编写python压力测试脚本; 2. 在命令行中启动Locust服务并指定压测脚本与hosts等信息; 3. 访问Locust本地网址设定并发量并进行压测。
2065 0
|
XML 应用服务中间件 数据库
图解WildFly8.x配置JNDI数据源
最近公司一直在推广使用JBoss,以前用的要么是Tomcat,要么是IBM的WAS,这里记录一下自己的转型过程,首先是安装Eclipse插件,具体步骤如下,如果是代理上网的,要在Eclipse里设置代理。
1325 0
|
云安全 安全 网络安全
|
4天前
|
JavaScript 安全 前端开发
Nodejs Brotli decoder 代码执行漏洞 (CVE-2026-111497)10629
Node.js发布安全更新修复Brotli解码器高危RCE漏洞(CVE-2026-111497),建议受影响版本用户立即升级。
1860 1
|
10天前
|
人工智能 API 开发工具
DeepSeek Harness 神插件:白嫖 13 家大厂模型
`dsh-our-free-model` 是 DeepSeek Harness 神级插件:零配置、零API Key、零账号,聚合13家大厂免费模型(含DeepSeek V4.1 Flash、Kimi K3等),支持本地网关、用量看板与自动降级,真正实现“白嫖即用”。
1687 37
|
4天前
|
安全 应用服务中间件 Go
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
PHP2Shell 双 RCE 已遭在野利用:WordPress、Drupal 全在射程内,nginx 其实另有洞。拆成因与修复顺序
PHP2Shell 漏洞拆解:WordPress 与 nginx 为何中招
|
7天前
|
Web App开发 人工智能 前端开发
2026 年 AI 浏览器怎么选?Ego Lite、Tabbit、Chrome DevTools MCP 深度对比,开发者到底该用哪个?
2026年AI浏览器选型指南:Ego Lite专注多Agent协同(Mac优先),Tabbit国产首选,原生支持Windows与Codex控制;Chrome DevTools MCP非浏览器而是开发者利器,深度集成Console/Network/Performance调试能力,让AI真正“看懂并验证”前端代码。
967 2
|
11天前
|
安全 Linux 芯片
【万字长文】Ollama下载、安装、配置、使用保姆级教程(2026最新图解)
Ollama 是一款免费开源的本地大模型运行工具,支持 DeepSeek、Qwen 等主流模型,一条命令即可下载运行。2026 最新版 v0.35.1 官方安装包(1.47GB)已提供国内直连下载,纯本地、离线可用、无广告、无隐私泄露风险。
1062 2