Ios 使用HttpClient子类的请求管理EnqueueBatchOfHttPrequesStoperationsWithRequests
我想使用AFNetworking和这个AFHTTPClient子类从一个web服务实现多个Json请求,以创建一个表视图。我将在MainViewController中创建tableViewIos 使用HttpClient子类的请求管理EnqueueBatchOfHttPrequesStoperationsWithRequests,ios,objective-c,request,afnetworking,Ios,Objective C,Request,Afnetworking,我想使用AFNetworking和这个AFHTTPClient子类从一个web服务实现多个Json请求,以创建一个表视图。我将在MainViewController中创建tableView #import "AFHTTPClient.h" @interface YlyHTTPClient : AFHTTPClient + ( YlyHTTPClient *)sharedHTTPClient; - (id)initWithBaseURL:(NSU
#import "AFHTTPClient.h"
@interface YlyHTTPClient : AFHTTPClient
+ ( YlyHTTPClient *)sharedHTTPClient;
- (id)initWithBaseURL:(NSURL *)url;
@end
#import "YlyHTTPClient.h"
static NSString * const urlString = @"http://localhost/example/";
@implementation YplyHTTPClient
+ (YlyHTTPClient *)sharedHTTPClient {
static YeeplyHTTPClient *_httpClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_httpClient = [[YlyHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
[_httpClient setParameterEncoding:AFJSONParameterEncoding];
[_httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
});
return _httpClient;
}
-(id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
return self;
}
首先,我尝试从MainViewController调用EnqueueBatchOfHttPrequesStoperationsWithRequest方法,执行以下操作:
- (void)viewDidLoad
{
NSMutableArray *mutableRequests = [NSMutableArray array];
for (NSString *URLString in [NSArray arrayWithObjects:@"users", @"projects", @"interestedUsers", nil]) {
[mutableRequests addObject:[[YlyHTTPClient sharedHTTPClient] requestWithMethod:@"GET" path:URLString parameters:nil]];
}
[[YlyHTTPClient sharedHTTPClient] enqueueBatchOfHTTPRequestOperationsWithRequests:mutableRequests progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%lu of %lu Completed", (unsigned long)numberOfCompletedOperations, (unsigned long)totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"Completion: %@", [operations objectAtIndex:1]);
}];
[super viewDidLoad];
}
我从NSLog得到的结果是:
Completion: <AFJSONRequestOperation: 0x75dbe60, state: isFinished, cancelled: NO request: <NSMutableURLRequest http://localhost/yeeply_service/api/example/projects>, response: <NSHTTPURLResponse: 0x72cd000>>
谢谢。依次回答您的问题: 我的第一个问题是,我如何从NSArray操作系统获取数据 您可以从集合中每个操作的
responseJSON
属性中获取:
for (AFJSONRequestOperation *operation in operations) {
id JSON = operation.responseJSON;
}
我的第二个问题是关于在我的AFHTTClient子类中实现这个方法,并使用委托从ViewController调用它,直接在ViewController中接收数据,这样我就可以管理这个数据并在tableView中设置它
在您的子类中,使用回调块参数创建一个新方法,其中包含您需要的信息,并在批处理完成块的末尾调用该块。问题是什么?我不明白你在问什么。请说清楚一点。谢谢。对不起,实际上我有两个问题。首先,我如何从NSArray操作中得到的响应中获取数据,这些操作在开始时显示在NSLog中。第二,如何管理AFHTTPClient子类中的EnqueueBatchOfHttPrequesStoperationsWithRequests方法,并以委托的形式将数据返回给ViewController。请编辑您的问题,同时提供这两种方法。试着更好地解释你将取得的成就。提前谢谢,希望你能理解。我很抱歉。如果你不能理解,我会努力做得更好,但我想我知道这是相当好的。谢谢你的关心。尽管如此,你还是应该听从老师的建议。即使这是你问题的答案,也可能不是解决问题的正确方法。
for (AFJSONRequestOperation *operation in operations) {
id JSON = operation.responseJSON;
}