iOS資料請求超時處理
前言
在網路情況不好時,afnet庫請求的預設超時時間過長,為了提高使用者體驗,我們可以自己設定請求超時時間,並在超時後取消當前請求佇列。
實現思路
1.在自己封裝的資料請求單利類中設定合適的請求超時時間;
[qlmanager.SessionManager.requestSerializer setTimeoutInterval:10];
2.請求超時後,會呼叫請求失敗的回撥,在回撥方法中判斷錯誤碼為-1001 時 進行超時提示以及取消當前請求任務
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
if (error.code == -1001)
{
[ql_hud_manager showInfoWithStatus:@"連線超時"];
[ql_hud_manager dismissWithDelay:2];
[task cancel];
handle(nil,error,0);
}
具體程式碼
#import "ql_http_manager.h"
#import "AFHTTPSessionManager.h"
@interface ql_http_manager()
@property(nonatomic,strong) AFHTTPSessionManager * SessionManager;
@end
@implementation ql_http_manager
static ql_http_manager * qlmanager = nil;
+ (instancetype)share_ql_http_manager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (!qlmanager)
{
qlmanager = [[ql_http_manager alloc]init];
}
});
return qlmanager;
}
- (instancetype)init
{
if ((qlmanager = [super init]))
{
qlmanager.SessionManager = [AFHTTPSessionManager manager];
qlmanager.SessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
qlmanager.SessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
[qlmanager.SessionManager.requestSerializer setTimeoutInterval:10];
}
return qlmanager;
}
+ (void)post_with_Url:(NSString *)url and_param:(NSDictionary *)param and_log:(NSString*)log and_handle:(SucBlock)handle
{
[self share_ql_http_manager];
[ql_hud_manager showWithStatus:log];
[qlmanager.SessionManager POST:url parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSDictionary * data = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
if ([data[@"status"] isEqualToString:@"200"])
{
[ql_hud_manager dismiss];
handle(data,nil,0);
}else if ([data[@"status"] isEqualToString:@"500"])
{
[ql_hud_manager showInfoWithStatus:@"伺服器程式異常"];
handle(data,nil,1);
}else if ([data[@"status"] isEqualToString:@"400"])
{
[ql_hud_manager showInfoWithStatus:@"請求引數結構錯誤"];
handle(data,nil,1);
}else if ([data[@"status"] isEqualToString:@"404"])
{
[ql_hud_manager showInfoWithStatus:@"使用者不存在"];
handle(data,nil,1);
}else if ([data[@"status"] isEqualToString:@"405"])
{
[ql_hud_manager showInfoWithStatus:@"密碼錯誤"];
handle(data,nil,1);
}else if ([data[@"status"] isEqualToString:@"406"])
{
[ql_hud_manager showInfoWithStatus:@"介面非法訪問"];
handle(data,nil,1);
}else if ([data[@"status"] isEqualToString:@"407"])
{
[ql_hud_manager showInfoWithStatus:@"資料庫資料異常"];
handle(data,nil,1);
}
NSLog(@"%@ %s 第%d行 %@",log,__func__,__LINE__,data[@"message"]);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
if (error.code == -1001)
{
[ql_hud_manager showInfoWithStatus:@"連線超時"];
[ql_hud_manager dismissWithDelay:2];
[task cancel];
handle(nil,error,0);
}
else
{
handle(nil,error,0);
[ql_hud_manager showInfoWithStatus:@"無法連線伺服器"];
NSLog(@"%s 第%d行 %@",__func__,__LINE__,error.description);
}
}];
}
@end
相關文章
- 請求資料處理
- spring security:ajax請求的session超時處理SpringSession
- SpringBoot可以同時處理多少請求?Spring Boot
- iOS for 迴圈內網路請求的處理iOS內網
- 大請求、請求超時問題
- 關於在request請求時,處理請求引數的問題
- springmvc處理ajax請求SpringMVC
- ajax呼叫WebMethed返回處理請求時出錯Web
- 理解 Nginx 在處理請求時的匹配規則Nginx
- DeferredResult——非同步請求處理非同步
- Django爬蟲:如何處理超過重試次數的請求以保障資料完整性Django爬蟲
- 超3萬億資料實時分析,JCHDB助力海量資料處理
- nginx 如何處理一個請求Nginx
- Go Web如何處理Web請求?GoWeb
- yai 請求預處理指令碼AI指令碼
- Netty(二):如何處理io請求?Netty
- Laravel請求處理管道理解Laravel
- 處理 HTTP 請求的註解HTTP
- springmvc原始碼 ---DispatcherServlet 處理請求SpringMVC原始碼Servlet
- 精講響應式WebClient第5篇-請求超時設定與異常處理Webclient
- axios請求超時解決方案iOS
- crmeb系統請求介面超時,超過十秒就請求失敗
- 處理請求(AFURLRequestSerialization)和響應(AFURLResponseSerialization)
- 使用cors完成跨域請求處理CORS跨域
- linux如何處理多連線請求?Linux
- SpringBoot 教程之處理非同步請求Spring Boot非同步
- 4、Ktor學習-處理HTTP請求;HTTP
- java webservice 帶請求頭方式處理JavaWeb
- Spring MVC的請求處理邏輯SpringMVC
- 搜尋 伺服器處理請求伺服器
- options 請求跨域問題處理跨域
- Nacos - 服務端處理心跳請求服務端
- axios請求超時,設定重新請求的完美解決方法iOS
- C# 優雅的處理TCP資料(心跳,超時,粘包斷包,SSL加密 ,資料處理等)C#TCP加密
- 答疑 - SAP OData 框架處理 Metadata 後設資料請求的實現細節框架
- Nginx請求處理流程你瞭解嗎?Nginx
- axios中POST請求變成OPTIONS處理iOS
- Node.js如何處理多個請求?Node.js
- 封裝springmvc處理ajax請求結果封裝SpringMVC