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

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

本文介绍了AFNetworking在GET请求的JSON参数中发送数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将数组作为AFNetworking GET请求中的参数发送.

I am sending array as a parameter in AFNetworking GET Request.

我的代码如下:

- (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]];
        }
    }];
}

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

The dictionary which I pass as parameter is as follows:

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

创建的URL显示为

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中的问题区域是数组未以正确的格式传递,即

The problem area in URL is array is not passed in proper format i.e.

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

有人可以告诉我如何解决这个错误吗?

Can anyone tell me how to solve this mistake?

但是我没有收到成功的答复.

But I am not getting the successful response.

推荐答案

在afnetworking 3.0中,请使用AFHTTPSessionManager

In afnetworking 3.0, instead of AFHTTPRequestOperationManager use 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);
}];

这篇关于AFNetworking在GET请求的JSON参数中发送数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 12:07