@interface AlivcAppServer : NSObject
/**
get請(qǐng)求的封裝
本應(yīng)用所有的get請(qǐng)求最終都通過(guò)這個(gè)方法發(fā)起
@param urlString 請(qǐng)求字符串
@param handle 請(qǐng)求結(jié)果的處理代碼塊 errString不為空,則代表請(qǐng)求出錯(cuò)
*/
+ (void)getWithUrlString:(NSString *)urlString completionHandler:(void (^)(NSString *__nullable errString,NSDictionary *_Nullable resultDic))handle;
/**
post請(qǐng)求的封裝
本應(yīng)用所有的get請(qǐng)求最終都通過(guò)這個(gè)方法發(fā)起
@param urlString 請(qǐng)求字符串
@param parametersDic 參數(shù)
@param handle 請(qǐng)求結(jié)果的處理代碼塊 errString不為空血当,則代表請(qǐng)求出錯(cuò)
*/
+ (void)postWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parametersDic completionHandler:(void (^)(NSString *__nullable errString,NSDictionary *_Nullable resultDic))handle;
/**
統(tǒng)一處理返回的結(jié)果字典
@param resultDic 結(jié)果字典
@param success 結(jié)果字典表明請(qǐng)求成功少辣,那么解析出數(shù)據(jù)字典讓別人使用
@param failure 失敗
*/
+ (void)judgmentResultDic:(NSDictionary *)resultDic success:(void (^)(id dataObject))success doFailure:(void (^)(NSString *))failure;
#import <AFNetworking/AFNetworking.h>
@implementation AlivcAppServer
// 單例會(huì)解決請(qǐng)求類內(nèi)存泄露的問(wèn)題
+ (AFHTTPSessionManager*) manager
{
static dispatch_once_t onceToken;
static AFHTTPSessionManager *manager = nil;
dispatch_once(&onceToken, ^{
manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/javascript",@"text/html",@"text/plain",nil];
manager.requestSerializer=[AFHTTPRequestSerializer serializer];
});
return manager;
}
+ (void)getWithUrlString:(NSString *)urlString completionHandler:(void (^)(NSString * _Nullable, NSDictionary * _Nullable))handle{
[[self manager] GET:urlString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *resultDic = (NSDictionary *)responseObject;
handle(nil,resultDic);
}else{
handle(@"數(shù)據(jù)格式異常",nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
handle(error.description,nil);
}];
}
+ (void)postWithUrlString:(NSString *)urlString parameters:(NSDictionary *)parametersDic completionHandler:(void (^)(NSString * _Nullable, NSDictionary * _Nullable))handle{
[[self manager] POST:urlString parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *resultDic = (NSDictionary *)responseObject;
handle(nil,resultDic);
}else{
handle(@"數(shù)據(jù)格式異常",nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
handle(error.description,nil);
}];
}
/**
統(tǒng)一處理返回的結(jié)果字典
@param resultDic 結(jié)果字典
@param success 結(jié)果字典表明請(qǐng)求成功炉爆,那么解析出數(shù)據(jù)字典讓別人使用
@param failure 失敗
*/
+ (void)judgmentResultDic:(NSDictionary *)resultDic success:(void (^)(id dataObject))success doFailure:(void (^)(NSString *))failure{
BOOL isSucess = [resultDic[@"result"] boolValue];
if (isSucess) {
id dataObject = resultDic[@"data"];
success(dataObject);
}else{
NSString *messageString = resultDic[@"message"];
failure(messageString);
}
}