2013-03-25 12 views
0

我試圖通過Clear Read API(基本上從網址中提取文章部分)運行一堆網址,並使用AFNetworking Library使用AFNetworking和AFHTTPClient通過API運行多個URL - 我做錯了什麼?

我有一個AFClearReadClient類,它是AFHTTPClient的一個子類,我使用它來簡化與API的交互。在那裏我設置了基本的URL和它是JSON請求的事實。

#import "AFClearReadClient.h" 
#import "AFJSONRequestOperation.h" 

@implementation AFClearReadClient 

+ (AFClearReadClient *)sharedClient { 
    static AFClearReadClient *sharedClient = nil; 
    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     sharedClient = [[AFClearReadClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.thequeue.org/v1/clear?url=&format="]]; 
    }); 

    return sharedClient; 
} 

- (id)initWithBaseURL:(NSURL *)url { 
    if (self = [super initWithBaseURL:url]) { 
     [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
     [self setDefaultHeader:@"Accept" value:@"application/json"]; 
    } 

    return self; 
} 

那麼我存儲在一個NSDictionary它通過我環路的文章列表,讓每一篇文章的URL,從它即將到來的請求作出參數(參數是GET變量及其值基本URL,對嗎?),然後創建請求,並將其添加到包含所有請求的數組中。

然後我批量排隊他們(我認爲我沒有做對)。這會創建並排入每個請求,這會將其置於執行過程中,對嗎?但是我在progressBlock中做了什麼?我沒有訪問返回的JSON(看起來,本地變量只是NSUIntegers),所以我不能做我想做的事情(保存返回的文章文本)。

- (void)addArticlesToQueueFromList:(NSDictionary *)articles { 
    // Create an array to hold all of our requests to make 
    NSMutableArray *requests = [[NSMutableArray alloc] init]; 

    for (NSString *key in articles) { 
     NSString *articleURL = [[articles objectForKey:key] objectForKey:@"resolved_url"]; 
     NSDictionary *requestParameters = @{@"url": articleURL, 
              @"format": @"json"}; 

     NSMutableURLRequest *request = [[AFClearReadClient sharedClient] requestWithMethod:@"GET" path:nil parameters:requestParameters]; 
     [requests addObject:request]; 
    } 

// [[AFClearReadClient sharedClient] setMaxConcurrentOperationCount:5]; 
    [[AFClearReadClient sharedClient] enqueueBatchOfHTTPRequestOperationsWithRequests:[requests copy] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) { 

    } completionBlock:^(NSArray *operations) { 

    }]; 
} 

此外,我不知道我應該如何使用setMaxConcurrentOperationCount:方法。這應該在AFClearReadClient課上完成嗎?

+0

也許你應該使用'enqueueBatchOfHTTPRequestOperations:progressBlock:completionBlock:' – tassar 2013-03-25 03:59:59

+1

這實際上並沒有什麼不同的方法,它只是使得它必須在使用該方法之前創建請求操作,而使用那個方法將從您的請求中爲您創建。 – 2013-03-25 04:06:03

回答

1

首先,你排隊的請求,而不是requestOperations - 有一個區別。注意函數的名稱(enqueueBatchOfHTTPRequestOperations)。你直到請求步驟才行 - 然後你需要創建一個AFJSONRequestOperation。所以,一旦你的請求,請執行下列操作:

AFJSONRequestOperation *requestOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){ ...this is your success block...} 
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON){ ...this is your failure block...}]; 

現在你已經有了一個requestOperation,你可以繼續排隊認爲,它應該工作。

+0

你確定嗎?在我正在使用的方法定義(http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/enqueueBatchOfHTTPRequestOperationsWithRequests:progressBlock:completionBlock :)它說:「創建併入隊AFHTTPRequestOperation 「,這似乎不同意你說的話。使用您的選項,我正在創建請求,但不是像我想要的那樣使用批處理類型的方法。 – 2013-03-25 12:25:51

+0

啊,確定 - 我的不好。你是正確的使用請求,但通過使用它,它將成功和完成塊設置爲零。你可以做的是使用'setCompletionBlockWithSuccess:failure:'手動設置每個請求的成功/失敗塊,這應該讓你訪問響應JSON。 – 2013-03-26 10:06:02