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

你可能感兴趣的:(iOS数据请求超时处理)