相信很多小伙伴都在為進行網(wǎng)絡(luò)請求的數(shù)據(jù)處理之類的煩惱吧,下面就為小伙伴們分享自己開發(fā)過程中的網(wǎng)絡(luò)請求工具類吧,直接上源碼(這里的 JYLOG是自定義的輸出,小伙伴們可以替換或者注釋)
+ (void)networkRequestGET:(NSString *)url
parameters:(NSDictionary *)parameters
success:(void(^)(id object))success
failure:(void(^)(id error))failure{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
[manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
responseObject = WWJSONObjectResetNullValue(responseObject, NSJSONReadingAllowFragments);
NSDictionary *state = responseObject[kResponseState];
NSInteger status = [state[kResponseStatus]integerValue];
if(status == 0) {
JYLOG(@"--- 操作成功 --- :%@", responseObject);
success(responseObject[kResponseData]);
} else {
JYLOG(@"--- 操作失敗 --- :%@", responseObject);
failure(state[kResponseMessage]);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(@"網(wǎng)絡(luò)連接錯誤");
JYLOG(@"%@", error);
}];
}
+ (void)networkRequestPOST:(NSString *)url
parameters:(NSDictionary *)parameters
success:(void(^)(id object))success
failure:(void(^)(id error))failure
{
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"https" ofType:@"cer"];
NSData * certData =[NSData dataWithContentsOfFile:cerPath];
NSSet * certSet = [[NSSet alloc] initWithObjects:certData, nil];
AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
[securityPolicy setAllowInvalidCertificates:YES];
[securityPolicy setPinnedCertificates:certSet];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
NSLog(@"請求URL:%@\n請求參數(shù):%@", url, parameters);
[manager POST:url parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSDictionary class]]) {
responseObject = WWJSONObjectResetNullValue(responseObject, NSJSONReadingAllowFragments);
NSDictionary *state = responseObject[kResponseState];
NSInteger status = [state[kResponseStatus]integerValue];
if(status == 0) {
NSLog(@"--- 操作成功 --- :%@", responseObject);
success(responseObject[kResponseData]);
} else {
NSLog(@"--- 操作失敗 --- :%@", responseObject);
failure(state[kResponseMessage]);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
failure(@"網(wǎng)絡(luò)連接錯誤");
JYLOG(@"%@", error);
}];
}
#pragma mark - Private
static id WWJSONObjectResetNullValue(id JSONObject, NSJSONReadingOptions readingOptions) {
if ([JSONObject isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];
for (id value in (NSArray *)JSONObject) {
[mutableArray addObject:WWJSONObjectResetNullValue(value, readingOptions)];
}
return (readingOptions & NSJSONReadingMutableContainers) ? mutableArray : [NSArray arrayWithArray:mutableArray];
} else if ([JSONObject isKindOfClass:[NSDictionary class]]) {
NSMutableDictionary *mutableDictionary = [NSMutableDictionary dictionaryWithDictionary:JSONObject];
for (id <NSCopying> key in [(NSDictionary *)JSONObject allKeys]) {
id value = (NSDictionary *)JSONObject[key];
if (!value || [value isEqual:[NSNull null]]) {
[mutableDictionary setObject:@"" forKey:key];
} else if ([value isKindOfClass:[NSArray class]] || [value isKindOfClass:[NSDictionary class]]) {
mutableDictionary[key] = WWJSONObjectResetNullValue(value, readingOptions);
}
}
return (readingOptions & NSJSONReadingMutableContainers) ? mutableDictionary : [NSDictionary dictionaryWithDictionary:mutableDictionary];
}
return JSONObject;
}
只要使用以上方法就可以對你的接口進行包裝請求數(shù)據(jù)了.