再次封装 AFNetworking

#import 
#import "AFNetworking.h"

typedef void(^NetworkingSuccess)(id response);
typedef void(^NetworkingFail)(NSError *error);

@interface NetworkingTools : NSObject
+ (void)postRequestWithURL:(NSString *)url parameters:(NSDictionary *)parameters success:(NetworkingSuccess)success fail:(NetworkingFail)fail;
+ (void)getRequestWithURL:(NSString *)url parameters:(NSDictionary *)parameters success:(NetworkingSuccess)success fail:(NetworkingFail)fail;
@end
#import "NetworkingTools.h"

@implementation NetworkingTools
+ (void)postRequestWithURL:(NSString *)url parameters:(NSDictionary *)parameters success:(NetworkingSuccess)success fail:(NetworkingFail)fail {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 添加 text/html 类型到可接收内容类型中
    manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/html", nil];
    [manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
}

+ (void)getRequestWithURL:(NSString *)url parameters:(NSDictionary *)parameters success:(NetworkingSuccess)success fail:(NetworkingFail)fail {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes= [NSSet setWithObjects:@"text/html", nil];
    [manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
        success(responseObject);
    }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        fail(error);
    }];
}
@end

你可能感兴趣的:(代码工具,实用知识)