iOS) 使用 AFHTTPRequestOperation 和 'GET' 方法获取响应时如何设置参数

iOS) How to set parameter when getting response using AFHTTPRequestOperation with 'GET' method

我的代码如下:

NSString *urlString = [[NSString stringWithFormat:@"/players?skip=%ld",(long)skipSize] DVURL];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[request set]
[request setValuesForKeysWithDictionary:[filter filteringDictionary]];
AFHTTPRequestOperation *operation =
[[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:
 ^(AFHTTPRequestOperation *operation, id responseObject) {
     NSLog(@"responseObject %@", responseObject);

     totalPages = ([[responseObject
                     objectForKey:@"count"] intValue] / 20);
     NSLog(@"%@",[responseObject objectForKey:@"players"]);
     for (NSDictionary *playerDict in [responseObject objectForKey:@"players"]) {
         DVPlayer *player = [[DVPlayer alloc] initWithDictionary:playerDict];
         if (![self.playerArray.players containsObject:player]) {
             [self.playerArray.players addObject:player];
         }
     }
     [playerCollectionView reloadData];

 } failure:
 ^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Error: %@", [error localizedDescription]);
     [[[UIAlertView alloc]
        initWithTitle:@"Error fetching players!"
        message:@"Please try again later"
        delegate:nil
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil] show];
 }];

[operation start];

当我使用 AFHTTPRequestOperationManager 时,我使用 NSDictionary 作为 GET 方法的参数。

所以我认为[request setValuesForKeysWithDictionary:[filter filteringDictionary]];是对应的。

但是我得到一个错误:

setValue:forUndefinedKey:]: this class is not key value coding-compliant for name(ex)

为了方便起见,我设计的参数是NSDictionary... 使用 AFHTTPRequestOperation 而不是 AFHTTPRequestOperationManager 时,我不能将参数设置为 NSDictionary 吗?

请帮帮我

AFHTTPRequestOperationNSURLRequest 作为初始参数。因此,您需要自己处理序列化,将其附加到您的 url,然后对 url:

进行编码
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[yourUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

AFHTTPRequestOperationManager 更简单的另一个原因 ;)