AFNetworking的封装

标签(空格分隔): 常用封装


AFNetworking是一个很重要的网络请求框架,如果用了它基本上程序的很多地方都需要用到它,为了防止AFNetworking更新以后,很多API接口无法使用,导致整个程序都需要改动。所以我们需要对AFNetworking这种第三方框架做一层屏蔽处理。以下做了简单的屏蔽处理

JLHttpTool.h文件


#import 

@interface BXHttpTool : NSObject

#pragma mark - 二级封装
+ (void)getResuestWithParams:(NSDictionary *)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

+ (void)postResuestWithParams:(NSDictionary *)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;


#pragma mark - 一级封装
+ (void)postUrl:(NSString *)urlStr params:(NSDictionary *)params progress:(void (^)(NSProgress *progress))progress success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

+ (void)getUrl:(NSString *)urlStr params:(NSDictionary *)params progress:(void (^)(NSProgress *progress))progress  success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;

@end

JLHttpTool.m文件


#import "BXHttpTool.h"
#import 

@implementation BXHttpTool


#pragma mark - 二级封装

+(void)getResuestWithParams:(NSDictionary *)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    [BXHttpTool getUrl:GlobalRequestUrl params:params progress:nil success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}



+ (void)postResuestWithParams:(NSDictionary *)params success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure
{
    [BXHttpTool postUrl:GlobalRequestUrl params:params progress:nil success:^(id responseObject) {
        success(responseObject);
    } failure:^(NSError *error) {
        failure(error);
    }];
}


#pragma mark - 一级封装

+ (void)getUrl:(NSString *)urlStr params:(NSDictionary *)params progress:(void (^)(NSProgress *))progress success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        if (progress) {
            progress(downloadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)postUrl:(NSString *)urlStr params:(NSDictionary *)params progress:(void (^)(NSProgress *))progress success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager POST:urlStr parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        if (progress) {
            progress(uploadProgress);
        }
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }]; 
}

@end

你可能感兴趣的:(AFNetworking的封装)