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.
    }
}

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

目录
相关文章
|
iOS开发
iOS Error Domain=NSURLErrorDomain Code=-999
iOS Error Domain=NSURLErrorDomain Code=-999
156 0
微信分享报错 wxlog:Error:fail to load Keychain status:-25300 解决办法
微信分享报错 wxlog:Error:fail to load Keychain status:-25300 解决办法
2395 0
关于ml.exe编译时的错误提示:error A2138 和 error A2036
关于ml.exe编译时的错误提示:error A2138 和 error A2036
|
5月前
|
人工智能 C++ Windows
[NextJs] 解决 Failed to load SWC binary for win32/64
快速解决 Next.js 在 Windows 下运行时 SWC Binary 报错的方法,包括安装 Microsoft Visual C++ Redistributable 和确认处理器架构。
|
6月前
|
Python
IDA3.12版本的python,依旧报错IDAPython: error executing init.py.No module named ‘impRefer to the message win
IDA3.12版本的python,依旧报错IDAPython: error executing init.py.No module named ‘impRefer to the message win
|
编译器 Go 开发工具
JetBrains GoLand 以debug运行Go程序时出现could not launch process: decoding dwarf section info at offset 0x0: too short报错之保姆级别解决方案
JetBrains GoLand 以debug运行Go程序时出现could not launch process: decoding dwarf section info at offset 0x0: too short报错之保姆级别解决方案
276 0
|
Web App开发 前端开发 JavaScript
DevTools 无法加载 SourceMap 错误:状态代码 404,net::ERR_HTTP_RESPONSE_CODE_FAILURE
DevTools 无法加载 SourceMap 错误:状态代码 404,net::ERR_HTTP_RESPONSE_CODE_FAILURE
383 0
|
PyTorch 算法框架/工具 C++
导入pytorch报错:Redistributable is not installed...安装vc_redist.x64.exe报错:Error 1402:Could not open key..
导入pytorch报错:Redistributable is not installed...安装vc_redist.x64.exe报错:Error 1402:Could not open key..
导入pytorch报错:Redistributable is not installed...安装vc_redist.x64.exe报错:Error 1402:Could not open key..
CodeBlocks中运行出现undifined reference to std::cxxll:basic_string错误解决方案
CodeBlocks中运行出现undifined reference to std::cxxll:basic_string错误解决方案
560 0
CodeBlocks中运行出现undifined reference to std::cxxll:basic_string错误解决方案
关于 海思平台sample的demo中添加ffmpeg静态库(.a)报错误undefined reference toavpriv_pix_fmt_hps_avi等错误 的解决方法
关于 海思平台sample的demo中添加ffmpeg静态库(.a)报错误undefined reference toavpriv_pix_fmt_hps_avi等错误 的解决方法
关于 海思平台sample的demo中添加ffmpeg静态库(.a)报错误undefined reference toavpriv_pix_fmt_hps_avi等错误 的解决方法