先写两个Blcok代码块 用来表示成功和失败
typedef void (^HttpSuccessBlock) (id json);
typedef void (^HttpFailureBolck) (NSError *error);
//自定义请求体
//GET
* @param URL 请求的网址字符串
* @param params 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
+(void)getwithItemURL:(NSString *)URL params:(NSDictionary *)params Success:(HttpSuccessBlock)success Farams:(HttpFailureBolck )failure;
//POST
* @param URL 请求的网址字符串
* @param params 请求的参数
* @param success 请求成功的回调
* @param failure 请求失败的回调
+(void)postwithItemURL:(NSString *)URL params:(NSDictionary *)params Success:(HttpSuccessBlock)success Farams:(HttpFailureBolck )failure;
在自定义方法中设置解析类型
+(instancetype)shardincident
{
static RequestHttp *http=nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
http=[[RequestHttp alloc]initWithBaseURL:[NSURL URLWithString:ItemURL]];
http.responseSerializer=[AFJSONResponseSerializer serializer];
//解析类型
http.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"application/x-javascript",@"text/plain",@"image/gif", nil];
http.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
});
return http;
}
在封装的get post方法中
+(void)getwithItemURL:(NSString *)URL params:(NSDictionary *)params Success:(HttpSuccessBlock)success Farams:(HttpFailureBolck)failure
{
RequestHttp *manager=[RequestHttp shardincident];
[manager GET:URL parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable json) {
if (success == nil)return ;
success(json);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (failure == nil)return ;
failure(error);
}];
}
+(void)postwithItemURL:(NSString *)URL params:(NSDictionary *)params Success:(HttpSuccessBlock)success Farams:(HttpFailureBolck)failure
{
RequestHttp * manage =[RequestHttp shardincident];
[manage POST:URL parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (success == nil)return ;
NSLog(@"responseObject %@",responseObject);
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error == nil)return ;
failure(error);
}];
}