AFNetworking 参数(query,非body)包含数组

先看看需求,GET请求,ParameterType:query,orderStatus是一个数组


AFNetworking 参数(query,非body)包含数组_第1张图片
image.png

用afn 一般的GET请求 有数组时候 url 是这样

.../orders?...&orderStatus[]=1&orderStatus[]=2...

可是服务端那边获取不到orderStatus,且Android也没有问题,那就只能iOS端修改咯,问Android同事,他的链接是:

.../orders?...&orderStatus=1&orderStatus=2...

没有中括号的形式

解决:AFHTTPRequestSerializer中的方法

- (void)setQueryStringSerializationWithBlock:(NSString *(^)(NSURLRequest *, id, NSError *__autoreleasing *))block

在block中返回需要的格式

复制了 afn 自带的query处理的方法

AFQueryStringFromParameters(parameters)

只改一句话

[mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];

全部代码

@interface JMAFQueryStringPair : NSObject
@property (readwrite, nonatomic, strong) id field;
@property (readwrite, nonatomic, strong) id value;

- (instancetype)initWithField:(id)field value:(id)value;

- (NSString *)jm_URLEncodedStringValue;
@end

@implementation JMAFQueryStringPair

- (instancetype)initWithField:(id)field value:(id)value {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.field = field;
    self.value = value;
    
    return self;
}

- (NSString *)jm_URLEncodedStringValue {
    if (!self.value || [self.value isEqual:[NSNull null]]) {
        return AFPercentEscapedStringFromString([self.field description]);
    } else {
        return [NSString stringWithFormat:@"%@=%@", AFPercentEscapedStringFromString([self.field description]), AFPercentEscapedStringFromString([self.value description])];
    }
}

@end

NSString * JM_AFQueryStringFromParameters(NSDictionary *parameters) {
    NSMutableArray *mutablePairs = [NSMutableArray array];
    for (JMAFQueryStringPair *pair in JM_AFQueryStringPairsFromDictionary(parameters)) {
        [mutablePairs addObject:[pair jm_URLEncodedStringValue]];
    }
    
    return [mutablePairs componentsJoinedByString:@"&"];
}

NSArray * JM_AFQueryStringPairsFromDictionary(NSDictionary *dictionary) {
    return JM_AFQueryStringPairsFromKeyAndValue(nil, dictionary);
}

NSArray * JM_AFQueryStringPairsFromKeyAndValue(NSString *key, id value) {
    NSMutableArray *mutableQueryStringComponents = [NSMutableArray array];
    
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"description" ascending:YES selector:@selector(compare:)];
    
    if ([value isKindOfClass:[NSDictionary class]]) {
        NSDictionary *dictionary = value;
        // Sort dictionary keys to ensure consistent ordering in query string, which is important when deserializing potentially ambiguous sequences, such as an array of dictionaries
        for (id nestedKey in [dictionary.allKeys sortedArrayUsingDescriptors:@[ sortDescriptor ]]) {
            id nestedValue = dictionary[nestedKey];
            if (nestedValue) {
                [mutableQueryStringComponents addObjectsFromArray:JM_AFQueryStringPairsFromKeyAndValue((key ? [NSString stringWithFormat:@"%@[%@]", key, nestedKey] : nestedKey), nestedValue)];
            }
        }
    } else if ([value isKindOfClass:[NSArray class]]) {
        NSArray *array = value;
        for (id nestedValue in array) {
            // afn 原来代码
            [mutableQueryStringComponents addObjectsFromArray:AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@[]", key], nestedValue)];
            [mutableQueryStringComponents addObjectsFromArray:JM_AFQueryStringPairsFromKeyAndValue([NSString stringWithFormat:@"%@", key], nestedValue)];
        }
    } else if ([value isKindOfClass:[NSSet class]]) {
        NSSet *set = value;
        for (id obj in [set sortedArrayUsingDescriptors:@[ sortDescriptor ]]) {
            [mutableQueryStringComponents addObjectsFromArray:JM_AFQueryStringPairsFromKeyAndValue(key, obj)];
        }
    } else {
        [mutableQueryStringComponents addObject:[[JMAFQueryStringPair alloc] initWithField:key value:value]];
    }
    
    return mutableQueryStringComponents;
}

然后在AFHTTPSessionManager创建时候 添加代码

static AFHTTPSessionManager *httpManager = nil;
+ (AFHTTPSessionManager *)shareHttpManager {
    if (!httpManager) {
        httpManager = [AFHTTPSessionManager manager];
        // ... 省略 ...
        [httpManager.requestSerializer setQueryStringSerializationWithBlock:^NSString *(NSURLRequest *request, id parameters, NSError *__autoreleasing *error) {
            return JM_AFQueryStringFromParameters(parameters);
        }];
        // ... 省略 ...
    }
    return httpManager;
}

搞定

你可能感兴趣的:(AFNetworking 参数(query,非body)包含数组)