一、在自己封装的AFNetWorking中添加移除null的字段
//需要在网络请求中添加的字段
AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
response.removesKeysWithNullValues = YES;
manager.responseSerializer = response;
+ (void)be_basePOSTRequestModel:(BEBaseRequestModel *)model url:(NSString *)url finishBlock:(FinishBlcok)finishBlock {
NSLog(@"requestDict = %@",model.keyValues);
NSLog(@"requestUrl = %@",url);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *utf = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.requestSerializer.timeoutInterval = 20;
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", nil];
AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
response.removesKeysWithNullValues = YES;
manager.responseSerializer = response;
[manager POST:utf parameters:model.keyValues progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
debugLog(@"responseObject = %@", responseObject);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSDictionary *JSON = nil;
if(responseObject != nil){
if ([responseObject isKindOfClass:[NSDictionary class]]) {
JSON = responseObject;
}else {
JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:kNilOptions error:nil];
NSLog(@"posT response: %@", JSON);
}
finishBlock(JSON,nil);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error = %@",error);
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
finishBlock(nil,error);
[self showError:error.localizedDescription];
}];
}
二、在AFNetWorking源代码中修改
static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingOptions readingOptions) {
if ([JSONObject isKindOfClass:[NSArray class]]) {
NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:[(NSArray *)JSONObject count]];
for (id value in (NSArray *)JSONObject) {
[mutableArray addObject:AFJSONObjectByRemovingKeysWithNullValues(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]]) {
//需要把所对应的null值替换为@""
mutableDictionary[key] = AFJSONObjectByRemovingKeysWithNullValues(value, readingOptions);
}
}
return (readingOptions & NSJSONReadingMutableContainers) ? mutableDictionary : [NSDictionary dictionaryWithDictionary:mutableDictionary];
}
return JSONObject;
}