1
我用下面的代码来设置我的业务经理标题:AFHTTPRequestOperationManager的行动没有得到
requestSerializerJson = [AFJSONRequestSerializer serializer];
[requestSerializerJson setValue:[taskHandler getBranchesApiConstantForName:@"header_value_content_type"] forHTTPHeaderField:[taskHandler getBranchesApiConstantForName:@"header_name_content_type"]];
[requestSerializerJson setValue:[taskHandler getBranchesApiConstantForName:@"header_value_api_key"] forHTTPHeaderField:[taskHandler getBranchesApiConstantForName:@"header_name_api_key"]];
httpRequestOperationManager = [AFHTTPRequestOperationManager manager];
httpRequestOperationManager.requestSerializer = requestSerializerJson;
httpRequestOperationManager.responseSerializer = [AFJSONResponseSerializer serializer];
httpRequestOperationManager.operationQueue = [[NSOperationQueue alloc] init];
httpRequestOperationManager.operationQueue.maxConcurrentOperationCount = 1;
然后,我有下载多个JSON字符串的方法:
- (void)downloadBranches {
NSLog(@"Test %@", httpRequestOperationManager.requestSerializer.HTTPMethodsEncodingParametersInURI);
for (NSString *branchUuid in taskHandler.addedBranchesUuids) {
NSString *urlString = [NSString stringWithFormat:@"%@%@%@", urlBase, [taskHandler getBranchesApiConstantForName:@"getbranch_suffix"], branchUuid];
NSURL *URL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSLog(@"Test %@", request.allHTTPHeaderFields);
AFHTTPRequestOperation *op = [httpRequestOperationManager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[httpRequestOperationManager.operationQueue addOperation:op];
}
}
所有的操作都被执行,但是我从服务器收到一条消息,说我没有提供身份验证密钥。
我特别将其设置在第3行 - 对于requestSerializerJson
,然后将其添加到httpRequestOperationManager
,因此所有请求都应使用标头。
我添加了一个NSLog来打印request.allHTTPHeaderFields
,但它说“(null)”。
我该如何解决这个问题?
感谢您的澄清 - 我错了。但是,还有一个问题:当我使用AFHTTPSessionManager及其GET方法时,似乎没有办法确保所有请求都按顺序执行。 AFHTTPRequestOperationManager上的GET具有相同的效果,还是仅仅创建一个请求操作,然后我可以将它添加到操作队列中? – JakeP 2014-09-01 08:41:48
GET方法和当前代码都没有强制执行一次正在运行的操作。 – 2014-09-01 14:25:53
您可以将操作队列的最大并发操作属性设置为1;那么要么完成这一点。或者,您可以在获取所需数据的操作的完成处理程序中安排依赖操作。 – 2014-09-01 14:28:52