Domain = NSURLErrorDomain Code = -1005 'ネットワーク接続が中断されました。' AFNetWorkingを使用してエラーが発生しました



Domain Nsurlerrordomain Code 1005network Connection Has Been Interrupted



最近、AFNetworkingリクエストインターフェイスを使用して会社のプロジェクトを作成しましたが、頻繁に表示されましたDomain = NSURLErrorDomain Code = -1005 'ネットワーク接続が中断されました。このエラー。
自分で間違ったパラメータを書いたか、ネットワークが良くなかったと思いましたが、チェック後に除外されており、このエラーは確かではなく、良いか悪いかです。
同じタイプのエラーをオンラインで多数チェックし、信頼できる答えを見つけました。

http://blog.sina.com.cn/s/blog_12ec09c9c0102wxj8.html

iOS frequently appears Error Domain=NSURLErrorDomain Code=-1005 “The networ (2017-08-23 13:04:56)![](http://upload-images.jianshu.io/upload_images/1433510-9b48ce0f9c4dd998.gif?imageMogr2/auto-orient/strip)Reprint*▼* Category: [iOS Technology] (http://blog.sina.com.cn/s/articlelist_5079342236_1_1.html) Solution: The server modifies the KeepAliveTimeout parameter to 60s (for reference only). This is changed in our project, and this error has not occurred since then. Reference link: [http://stackoverflow.com/questions/25372318/error-domain-nsurlerrordomain-code-1005-the-network-connection-was-lost/25996971#25996971] (http://stackoverflow.com/questions /25372318/error-domain-nsurlerrordomain-code-1005-the-network-connection-was-lost/25996971#25996971)

リクエストに応答しておらず、接続が切断されているはずですが、インターフェースをリクエストしていません。
しかし、私が彼にそれを修正させるように背景に言ったとき、甌穴が来た場所、背景は彼がそうしないと言って、そのようなパラメータはないと言った、ピット。私たちのアプリケーションはLANアプリケーションであるため、バックグラウンドサービスはバックグラウンドでQTによって開発されます。インターネットでもチェックしました。変更するのは本当に簡単ではありません。私は長い間バックグラウンドで解決策を見つけていません。できません。自分でしかできません。 。
私の解決策は次のとおりです。インターフェイスが-1005を返したら、インターフェイスを再要求します(オンラインソリューションも参照してください)
パッケージ化されたAFNetWorkingインターフェイスは次のとおりです。

@interface XYNetworkingManager () / / Record the number of requests for the code -1005 connection //{key - method ,value - faildcount} @property (nonatomic, strong) NSMutableDictionary *code_1005_method_count_dic @end / / Request method -(void)PostWith_URL:(NSString *)url Param:(NSDictionary *)dic Method:(NSString *)method Timeout:(NSInteger)timeout Finsh:(XYNetBlock)Block { //signature NSDictionary *PostDic = [XYSignManager signXinyiWithDic:dic method:method] self.manager.requestSerializer.timeoutInterval = timeout [self.manager POST:url parameters:tempDic progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { if (responseObject == nil) { NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] XYLog (@' parsing failure: %@', result) } NSString *resStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] NSDictionary *dic = [resStr mj_JSONObject] Block(YES,dic) } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { XYLog (@' error message: %@', error) if (error.code == -1005) { XYLog (@'waiting 5S will re-request the task') / / This is a dictionary defined to record the name of the interface requesting the error and the number of errors self.code_1005_method_count_dic = [[NSMutableDictionary alloc] init] [self.code_1005_method_count_dic setObject:@(1) forKey:method] dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ dispatch_group_t downloadGroup = dispatch_group_create() dispatch_group_enter(downloadGroup) dispatch_group_wait(downloadGroup, dispatch_time(DISPATCH_TIME_NOW, 5000000000)) // Wait 5 seconds before trying again. dispatch_group_leave(downloadGroup) dispatch_async(dispatch_get_main_queue(), ^{ / / Re-request method [self rePostWith_URL:url Param:dic Method:method Timeout:15 Finsh:^(BOOL isSuccess, NSDictionary *responseDic) { if (isSuccess) { Block(YES,responseDic) } else { Block(NO,nil) } }] }) }) } else { Block(NO,nil) } }] } // -1005 re-request -(void)rePostWith_URL:(NSString *)url Param:(NSDictionary *)dic Method:(NSString *)method Timeout:(NSInteger)timeout Finsh:(XYNetBlock)Block { XYLog (@'task re-request') int recount = [self.code_1005_method_count_dic[method] intValue] recount++ [self.code_1005_method_count_dic setObject:@(recount) forKey:method] //signature NSDictionary *PostDic = [XYSignManager signXinyiWithDic:dic method:method] self.manager.requestSerializer.timeoutInterval = timeout [self.manager POST:url parameters:PostDic progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { XYLog (@'request for success') NSString *resStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] NSDictionary *dic = [resStr mj_JSONObject] Block(YES,dic) } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { if (error.code == -1005) { int count = [self.code_1005_method_count_dic[method] intValue] if (count >= 5) { XYLog (@'request exceeds limit, stop request') Block(NO,nil) return } else { [self rePostWith_URL:url Param:dic Method:method Timeout:15 Finsh:^(BOOL isSuccess, NSDictionary *responseDic) { if (isSuccess) { Block(YES,responseDic) } else { Block(NO,nil) } }] //return } } else { XYLog (@'re-request failed') Block(NO,nil) } }] }

このエラーが発生したときに-1005に遭遇し、それを記録し、5S後にこのインターフェイスを再要求し、5つの要求が失敗した場合、要求しなくなった場合、無限ループを防ぐために最大5回要求します。



問題を解決するためにこれを変更した後。

参照リンク: