我想實現來自Web服務與AFNetworking和此AFHTTPClient子類的多個Json請求來創建表視圖。我將在MainViewController中創建tableView。管理enqueueBatchOfHTTPRequestOperationsWithRequests AFHTTPClient子類
#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這樣調用enqueueBatchOfHTTPRequestOperationsWithRequest方法:
- (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>>
(我有三個NSMutableRequest,但我只在這裏展示一個)。
我的第一個問題是,我如何從操作NSArray中獲取數據? NSArray中是否沒有JSON響應的信息?如果有的話,我怎樣才能將它作爲字典來讀取?
在我AFHTTClient子類我的第二個問題是關於實現此方法,並使用委託,並在視圖控制器直接接收數據,這樣我可以管理這些數據,並將其設置在的tableView從視圖控制器調用它。
-(void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
謝謝。
問題是什麼?我不明白你在這裏問什麼。請更清楚。謝謝。 –
對不起,其實我有兩個問題。第一個問題,我如何從我在開始時顯示在NSLog中的NSArray操作中獲得的響應中獲取數據。其次,我如何管理AFHTTPClient子類中的enqueueBatchOfHTTPRequestOperationsWithRequests方法,並將委託中的數據返回給ViewController。 – roxeman3
請編輯您提供的問題。試着更好地解釋你會達到什麼。提前致謝。 –