AFNetworking 3.0 Code=-1016 錯誤解決方案
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 解決 Xcode 8中更新 SnapKit 3.0 的錯誤iOSXCodeAPK
- 使用JPEGImageEncoder提示Access restriction:錯誤 解決方案REST
- latex 錯誤以及解決方案
- 解決 Python UnicodeEncodeError 錯誤PythonUnicodeError
- 升級到XCode8,Swift3.0後,解決各種錯誤XCodeSwift
- Linux下錯誤解決方案Linux
- AFNetworking 支援cookies的解決方案Cookie
- Ubuntu Cannot allocate memory 錯誤解決方案Ubuntu
- ORA-28000錯誤解決方案
- ORA-07445: 錯誤解決方案
- ORA-00054錯誤解決方案(摘)
- 解決MySQL server has gone away錯誤的解決方案MySqlServerGo
- Hadoop常見錯誤及解決方案Hadoop
- HTTP 錯誤 500.21 - Internal Server Error 解決方案HTTPServerError
- MySQL插入資料1366錯誤解決方案MySql
- 開發常見錯誤及解決方案
- MySQL錯誤Incorrect file format解決方案薦MySqlORM
- Java java.lang.ExceptionInInitializerError 錯誤解決方案JavaExceptionError
- DataGuard ORA-10458錯誤解決方案
- informatica SORT_40406 錯誤解決方案ORM
- SQL Server 錯誤程式碼:17058 。解決方案SQLServer
- json.dumps錯誤:'utf8' codec can't decode byte解決方案JSON
- vagrant homestead 錯誤記錄以及解決方案
- JNI命令列下編譯錯誤解決方案命令列編譯
- ORA-00845的錯誤與解決方案
- Unsupported major.minor version 51.0 錯誤解決方案
- SVN 遇到的一些錯誤解決方案
- ORA-01658 錯誤之解決方案
- ant構建時出現錯誤解決方案
- PbootCMS中常見的錯誤提示及其解決方案boot
- TCP網路除錯助手提示錯誤:“1035:未知錯誤” 解決方案TCP除錯
- SSL錯誤ssl connect error 35的解決方案Error
- IIS 7.5 解析錯誤 命令執行漏洞解決方案
- java:錯誤:不支援發行版本1.4解決方案Java
- MySQL資料庫常見錯誤及解決方案MySql資料庫
- MySQL連線錯誤(10048)的解決方案MySql
- [轉]MySql錯誤程式碼1045的解決方案MySql
- CS0234 錯誤,奇葩bug,解決方案