2013-05-15 18 views
0

我想實現來自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; 

謝謝。

+0

問題是什麼?我不明白你在這裏問什麼。請更清楚。謝謝。 –

+0

對不起,其實我有兩個問題。第一個問題,我如何從我在開始時顯示在NSLog中的NSArray操作中獲得的響應中獲取數據。其次,我如何管理AFHTTPClient子類中的enqueueBatchOfHTTPRequestOperationsWithRequests方法,並將委託中的數據返回給ViewController。 – roxeman3

+0

請編輯您提供的問題。試着更好地解釋你會達到什麼。提前致謝。 –

回答

0

回答您的問題依次是:

我的第一個問題是,如何從操作NSArray中獲取數據?

您可以從每個操作的集合中的responseJSON屬性得到這樣的:

for (AFJSONRequestOperation *operation in operations) { 
    id JSON = operation.responseJSON; 
} 

我的第二個問題是關於實現此方法在我AFHTTClient子類,並使用委託從ViewController中調用它,並直接在ViewController中接收數據,以便我可以管理這些數據並將其設置在tableView中。

在你的子類,創建與您所需要的信息的回調塊參數的新方法,並調用該塊,在該批次完成塊的結束。

+0

這就是說,你應該真的按照[你原來的問題這個答案]的建議(http://stackoverflow.com/a/16564035/157142)。即使這是你的問題的答案,它可能不是解決你的問題的正確方法。 – mattt