最近在作文件上傳功能的時候遇到了一些問題,有關AFNetworking失敗的一些錯誤碼,這裏整理一下,以避免之後再踩坑:javascript
【1】錯誤碼-999,錯誤信息具體以下:html
Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLStringKey=https://test.com/xxxxxx, NSLocalizedDescription=cancelled, NSErrorFailingURLKey=https://test.com/xxxxxx}
緣由和代碼以下:java
AFHTTPSessionManager *manage = [[AFHTTPSessionManager alloc] init];; AFSecurityPolicy *policy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];//報錯時使用的證書校驗方式爲AFSSLPinningModeCertificate,但此時訪問的url證書校驗並未成功,因此更改成AFSSLPinningModeNone便可 policy.validatesDomainName = YES; manage.securityPolicy = policy;
【2】錯誤碼-1011,404,錯誤信息以下:nginx
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo={NSLocalizedDescription=Request failed: not found (404), NSErrorFailingURLKey=https://test.com/xxxxxx, com.alamofire.serialization.response.error.data=<>, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x60400c03ee80> { URL: https://test.com/xxxxxx } { Status Code: 404, Headers { Content-Type : [ application/octet-stream ], Content-Length : [ 0 ], Server : [ nginx ], Date : [ Wed, 16 May 2018 10:22:10 GMT ] } }}
看到這個錯誤碼請不要懷疑,這個就是你要訪問的url訪問不到所致使的,請檢查你的URLjson
【3】錯誤碼3840,400,錯誤信息以下服務器
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., NSUnderlyingError=0x6000058569e0 {Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo={NSLocalizedDescription=Request failed: bad request (400), NSErrorFailingURLKey=https://test.com/xxxxxx , com.alamofire.serialization.response.error.data=<3c68746d 6c3e0a3c 68656164 3e0a3c6d 65746120 68747470 2d657175 69763d22 436f6e74 656e742d 54797065 2220636f 6e74656e 743d2274 6578742f 68746d6c 3b206368 61727365 743d4953 20343030 3c2f6832 3e0a3c70 3e50726f 626c656d 20616363 65737369 6e67202f 772f6578 7465726e 616c2f75 706c6f61 6466696c 652e2052 6561736f 6e3a0a3c 7072653e 20202020 52657175 69726564 204d756c 74697061 72744669 6c652070 6172616d 65746572 20276669 6c652720 6973206e 6f742070 72657365 6e743c2f 7072653e 3c2f703e 3c687220 2f3e3c69 3e3c736d 616c6c3e 506f7765 72656420 6279204a 65747479 3a2f2f3c 2f736d61 6c6c3e3c 2f693e3c 62722f3e 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 20202020 0a3c6272 2f3e2020 >, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x60400c62b5a0> { URL: https://test.com/xxxxxx } { Status Code: 400, Headers { Date : [ Tue, 15 May 2018 01:22:33 GMT ], Content-Type : [ text/html;charset=ISO-8859-1 ], Content-Length : [ 1476 ], Cache-Control : [ must-revalidate,no-cache,no-store ], Server : [ nginx ] } }}}}
這是是你填充數據的問題,請檢查你的數據設置是否正確,剛出現這個問題的時候看描述覺得是服務器端返回的格式不是約定的json因此不能解析形成的,可是請服務端同事覈對了返回數據確實是json,而後懷疑如下返回數據可解析格式未設置@"text/json",檢查後發現也設置了app
AFHTTPSessionManager *manage = [[AFHTTPSessionManager alloc] init]; manage.responseSerializer = [[AFJSONResponseSerializer alloc] init]; manage.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"application/x-www-form-urlencoded", @"multipart/form-data", @"text/plain", @"image/jpeg", @"image/png", @"application/octet-stream",nil];
後來通過進一步的查閱資料發現,由於是文件上傳,因此直接使用了NSData拼接致使的問題,錯誤代碼示例以下:學習
NSData *data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMapped error:nil]; [[AFHTTPSessionManager shareInstance] POST:URL_KANO_UPLOADFILE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { [formData appendPartWithFormData:data name:@"file"];//此處不可這樣拼接 } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
而應該使用對應的mimeType類型作區分拼接ui
[[AFHTTPSessionManager shareInstance] POST:URL_KANO_UPLOADFILE parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { NSError *error; NSURL *fileURL = [NSURL fileURLWithPath:filePath]; [formData appendPartWithFileURL:fileURL name:@"file" fileName:fileName mimeType:@"image/jpeg" error:&error]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }];
具體的mimeType類型此處再也不贅述,請讀者自行查閱學習。url
【4】錯誤碼-1022,錯誤信息以下:
Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x60400025a640 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=https://test.com/xxxx, NSErrorFailingURLKey=https://test.com/xxxx, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}
出錯緣由:iOS9之後,蘋果把原http協議改爲了https協議,因此不能直接在http協議下GET/POST
解決辦法有兩種:
1.直接編輯工程文件下的Info.plist文件,加入如下代碼
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
2.在Xcode裏選中info.plist,點擊右邊的information Property List 後邊的加號,寫入App Transport Security Settings而後回車,先點擊左側展開箭頭,再點右側加號,Allow Arbitrary Loads 已經自動生成,直接回車,而後把默認值改成YES便可。其實這種方法等同於方法1.
【5】錯誤碼-1016,錯誤信息以下:
Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo={NSLocalizedDescription=Request failed: unacceptable content-type: text/plain, NSErrorFailingURLKey=https://test.com/xxxx, com.alamofire.serialization.response.error.data=<7b227374 61676522 3a226777 222c2263 6f646522 3a323030 3035312c 226d6573 73616765 223a22e7 adbee590 8de99499 e8afaf22 2c227375 63636573 73223a66 616c7365 7d>, com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x600000220640> { URL: https://test.com/xxxx } { Status Code: 200, Headers { Connection = ( "keep-alive" ); "Content-Encoding" = ( gzip ); "Content-Type" = ( "text/plain; charset=utf-8" ); Date = ( "Fri, 18 May 2018 07:14:25 GMT" ); Server = ( nginx ); "Transfer-Encoding" = ( Identity ); Vary = ( "Accept-Encoding" ); "X-Content-Type-Options" = ( nosniff ); "X-XSS-Protection" = ( 1 ); } }}
由於未設置AFHTTPSessionManager的requestSerializer的可接受文件類型,因此形成text/plain類型數據不可接受致使以上問題,進行下列設置便可:
manager.responseSerializer = [[AFJSONResponseSerializer alloc] init]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"application/x-www-form-urlencoded", @"multipart/form-data", @"text/plain", @"image/jpeg", @"image/png", @"application/octet-stream", nil];