1

嗨,我需要发送一个数组作为 Afnetworking 查询字符串中的参数之一

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL    URLWithString:@"http://192.008.0.28/aaa/a/"]];

    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: @"20", @"Miles", [NSArray arrayWithObjects:@"1",@"2",@"3",nil], @"Interval", nil];

    [httpClient postPath:iUpdateNotificationMethod parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"Request Successful, response '%@'", responseStr);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
    }];

但是服务器端我们得到了 "Miles":20,"Intervals":null 如何解决它谢谢,

4

3 回答 3

2

尝试这个

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:OAuthBaseURL];

NSMutableDictionary *parameters = [[NSMutableDictionary alloc] initWithCapacity:0];
    for (int i =0; i < [userIDs count]; i++) {
        NSString *userID = [[userIDs objectAtIndex:i] objectForKey:@"id"];
        NSDictionary *tmpDict = [NSDictionary dictionaryWithObjectsAndKeys:userID , [NSString stringWithFormat:@"ids[%i]",i], nil];
        [parameters addEntriesFromDictionary:tmpDict];
    }
    [client postPath:@"/user"
          parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSData *data = (NSData *)responseObject;
                NSString *jsonStr = [[NSString alloc] initWithData:data
                                                          encoding:NSUTF8StringEncoding];
                NSLog(@"jsonStr %@",jsonStr);

            }
            failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [self showError];
            }
     ];
于 2013-07-30T08:11:43.770 回答
0

由于您要提交一个数组,AFNetworking 会生成一个不同的参数名称并使用您提供的值对其进行重载。例如,您的请求会生成以下查询字符串:

Interval[]=1&Interval[]=2&Interval[]=3&Miles=20

这是AFHTTPClient.mAFQueryStringPairsFromKeyAndValue函数中定义的。

如果要保留原始参数,则应自行决定如何NSArray转换NSString。例如,您可以执行类似的操作[myArray componentsJoinedByString:@","],然后将其拆分回服务器上的元素。如果选择此方法,请注意使用可能出现在实际数据中的字符。

于 2013-07-16T05:44:42.230 回答
-1

我相信这会起作用: params = @{ @"Miles": @"20", @"Interval": @[@"1",@"2",@"3"] };

于 2013-07-16T05:46:26.763 回答