iOS开发 - OC之AFNetworking 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);
}];
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

或是用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)")
        }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

结果就妥妥地报错了

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
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

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

改为:

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

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

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

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

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

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

sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer]
 
 
  • 1
  • 1

Swift:

sessionManager.responseSerializer = AFHTTPResponseSerializer()
 
 
  • 1
  • 1

示例代码:

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.
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值