当前位置: 首页 > 工具软件 > Status HUD > 使用案例 >

一套包括网络请求、HUD/HUD点击取消请求api、get缓存、json转model解析的封装

高朝明
2023-12-01

LYNetworkRequestKit 是一款AFNetworking进行的二次封装的网络请求框架

  • 缓存策略,包括缓存时间、缓存更新机制等
  • 优先从网络还是缓存加载的设置,断网情况下自动从本地缓存请求
  • 网络请求的HUD,点击hud背景还可以取消某个api请求
  • 请求头httpHeader设置
  • 针对网络返回数据格式的设置、自定义字段
  • 网络请求回调可以自动解析成model或者modelArray,也会自动返回jsonData,只需要传入一个NSObject的类即可自动解析

1、基础使用方法:

[[ExamSubRequest shareInstance] requestGetJsonOperationWithParam:nil action:@"getDataList" showLoadHud:YES cancelEnable:YES normalResponse:^(NSInteger status, id  _Nonnull data) {
		//成功回调
    } exceptionResponse:^(NSError * _Nonnull error) {
    	//失败回调
    }];

2、针对单个请求设置网络请求设置、

ExamSubRequest * request1 =  [ExamSubRequest shareInstance];
    request1.loadCacheFirst = YES;//允许优先从缓存加载
    request1.refreshCache = YES;//允许刷新当前缓存
    [request1 requestGetJsonOperationWithParam:nil action:@"getDataList"
                                   showLoadHud:YES cancelEnable:YES
                                normalResponse:^(NSInteger status, id  _Nonnull data) {
        
    } exceptionResponse:^(NSError * _Nonnull error) {
        
    }];

3、对某个请求类设置全局请求设置、

//允许优先从缓存加载
-(BOOL)loadCacheFirst
{
    return YES;
}
//允许刷新当前缓存
- (BOOL)refreshCache{
    return YES;
}

下面方法允许直接网络请求数据解析成对应model返回数据给逻辑层

  • get返回一个model
-(void)requestGetJsonModelWithParam:(NSDictionary *)param
                              action:(NSString *)action
                         showLoadHud:(BOOL)showHud
                        cancelEnable:(BOOL)cancelEnable
                          modelClass:(Class)modelClass
                      normalResponse:(void(^)(NSInteger status, id data, NSObject *model))normalResponse
                   exceptionResponse:(void(^)(NSError *error))exceptionResponse;
  • get返回model数组
-(void)requestGetJsonArrayWithParam:(NSDictionary *)param
                              action:(NSString *)action
                         showLoadHud:(BOOL)showHud
                        cancelEnable:(BOOL)cancelEnable
                          modelClass:(Class)modelClass
                      normalResponse:(void(^)(NSInteger status, id data, NSMutableArray *array))normalResponse
                   exceptionResponse:(void(^)(NSError *error))exceptionResponse;
  • post返回一个model
--(void)requestPostJsonModelWithParam:(NSDictionary *)param
                               action:(NSString *)action
                          showLoadHud:(BOOL)showHud
                         cancelEnable:(BOOL)cancelEnable
                           modelClass:(Class)modelClass
                       normalResponse:(void(^)(NSInteger status, id data, NSObject *model))normalResponse
                    exceptionResponse:(void(^)(NSError *error))exceptionResponse;
  • post返回model 数组
-(void)requestPostJsonArrayWithParam:(NSDictionary *)param
                               action:(NSString *)action
                          showLoadHud:(BOOL)showHud
                         cancelEnable:(BOOL)cancelEnable
                           modelClass:(Class)modelClass
                       normalResponse:(void(^)(NSInteger status, id data, NSMutableArray *array))normalResponse
                    exceptionResponse:(void(^)(NSError *error))exceptionResponse;

Github、欢迎star 或者issue

 类似资料: