最近做公司的項(xiàng)目薪丁,使用AFNetworking請(qǐng)求接口遇西,頻繁出現(xiàn) Domain=NSURLErrorDomain Code=-1005 "網(wǎng)絡(luò)連接已中斷, 這個(gè)錯(cuò)誤严嗜。
開始以為是自己寫錯(cuò)了參數(shù)粱檀,或者網(wǎng)絡(luò)不好,但是檢查后都排除了 漫玄, 并且這個(gè)錯(cuò)誤不是一定的茄蚯,時(shí)好時(shí)壞。
在網(wǎng)上查閱不少同類型的錯(cuò)誤睦优,找到了一個(gè)靠譜的答案:
http://blog.sina.com.cn/s/blog_12ec09c9c0102wxj8.html
iOS 頻繁出現(xiàn) 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)轉(zhuǎn)載*▼*
分類: [iOS技術(shù)](http://blog.sina.com.cn/s/articlelist_5079342236_1_1.html)
解決辦法:服務(wù)端修改KeepAliveTimeout參數(shù)為60s(僅作參考)渗常,我們的項(xiàng)目中是這樣改的,此后再也沒出現(xiàn)此錯(cuò)誤汗盘。參考鏈接:[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)
應(yīng)該是請(qǐng)求還沒有得到回應(yīng)皱碘,連接就被斷開了,但是我們請(qǐng)求不到接口隐孽。
可是當(dāng)我把這個(gè)參數(shù)告訴后臺(tái)讓他修改一下的時(shí)候癌椿,坑爹的地方來(lái)了 , 后臺(tái)說(shuō)他不會(huì)菱阵,說(shuō)沒有這個(gè)參數(shù)踢俄,坑 。因?yàn)槲覀冞@個(gè)應(yīng)用是一個(gè)局域網(wǎng)應(yīng)用 送粱, 后臺(tái)服務(wù)是后臺(tái)用QT開發(fā)的褪贵, 我在網(wǎng)上也查了一下,確實(shí)不好修改抗俄,后臺(tái)吭哧吭哧半天脆丁,也沒找到解決方案 , 沒辦法动雹,只能自己動(dòng)手了槽卫。
我的解決方案是 :接口返回-1005的時(shí)候,重新請(qǐng)求接口 胰蝠, (也是參考網(wǎng)上的解決方法)
下面是封裝的AFNetWorking接口:
@interface XYNetworkingManager ()
//記錄code為-1005連接的請(qǐng)求次數(shù)
//{key - method ,value - faildcount}
@property (nonatomic, strong) NSMutableDictionary *code_1005_method_count_dic;
@end
//請(qǐng)求方法
-(void)PostWith_URL:(NSString *)url Param:(NSDictionary *)dic Method:(NSString *)method Timeout:(NSInteger)timeout Finsh:(XYNetBlock)Block
{
//簽名
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(@"解析失敗:%@",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(@"錯(cuò)誤信息:%@",error);
if (error.code == -1005) {
XYLog(@"等待5S將重新請(qǐng)求任務(wù)");
//這是定義的一個(gè)字典歼培,用來(lái)記錄請(qǐng)求錯(cuò)誤的的接口名以及錯(cuò)誤的次數(shù)
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(), ^{
//重新請(qǐng)求的方法
[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重新請(qǐng)求
-(void)rePostWith_URL:(NSString *)url Param:(NSDictionary *)dic Method:(NSString *)method Timeout:(NSInteger)timeout Finsh:(XYNetBlock)Block
{
XYLog(@"任務(wù)重新請(qǐng)求");
int recount = [self.code_1005_method_count_dic[method] intValue];
recount++;
[self.code_1005_method_count_dic setObject:@(recount) forKey:method];
//簽名
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(@"重新請(qǐng)求成功");
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(@"重新請(qǐng)求超過限制震蒋,停止請(qǐng)求");
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(@"重新請(qǐng)求失敗");
Block(NO,nil);
}
}];
}
思路就是遇到 -1005 這個(gè)錯(cuò)誤的時(shí)候,記錄下來(lái)躲庄,5S之后重新請(qǐng)求一下這個(gè)接口查剖,最多會(huì)請(qǐng)求5次 , 如果5次請(qǐng)求都失敗就不再請(qǐng)求了 噪窘, 防止死循環(huán)笋庄。
改成這樣之后就解決問題了 。
參考鏈接: