AFNetworking在GET请求的JSON参数中发送数组

时间:2015-10-26 06:38:27

标签: objective-c iphone json afnetworking afnetworking-2

我在AFNetworking GET请求中发送数组作为参数。

我的代码如下:

- (void)getProductSearchResult:(NSString *)locale andSearchDict:(NSDictionary *)dictSearch{

    NSString *strURL = [NSString stringWithFormat:@"%@/%@/search?%@",BASEURL,locale,APIFORMAT];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager GET:strURL parameters:dictSearch success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSDictionary *jsonDict = (NSDictionary *)responseObject;

        if ([jsonDict isKindOfClass:[NSDictionary class]] || [jsonDict isKindOfClass:[NSMutableDictionary class]]) {
            if (self.delegate  && [self.delegate respondsToSelector:@selector(API_ProductSearch_didSuccess:)]) {
                [self.delegate API_ProductSearch_didSuccess:jsonDict];
            }
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(api_ProductSearch_didFailWithError:)]) {
            [self.delegate api_ProductSearch_didFailWithError:[NSError description]];
        }
    }];
}

我作为参数传递的字典如下:

{
    "brand_filter" =     (
        1
    );
    "category_filter" =     (
        438
    );
    "max_price" = "47.37188";
    "min_price" = "1.95";
    "price_currency" = USD;
    "supplier_filter" =     (
        "Aakron Line"
    );
}

创建的网址显示为

http://demo.aakronline.ca/app_dev.php/api/v1/en_us/search?_format=json&brand_filter[]=1&category_filter[]=438&max_price=48.04479&min_price=2.622917&price_currency=USD&supplier_filter[]=Aakron%20Line

URL中的问题区域是不以正确的格式传递数组,即

brand_filter[]=1&category_filter[]=438 instead of brand_filter=[1]&category_filter=[438]

有谁能告诉我如何解决这个错误?

但我没有得到成功的回应。

1 个答案:

答案 0 :(得分:0)

在afnetworking 3.0中,而不是AFHTTPRequestOperationManager使用AFHTTPSessionManager

NSString *strUrl = [NSString stringWithFormat:@"%@/%@/search?%@",BASEURL,locale,APIFORMAT];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
manager.responseSerializer = [AFJSONResponseSerializer serializer];

[manager GET:strUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"value: %@",responseObject);

 //other code as it is

} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    NSLog(@"Error: %@",error);
}];