ios - 使用 AFNetworking 和 AFHTTPClient 通过 API 运行许多 URL - 我做错了什么?

标签 ios objective-c xcode ios6 afnetworking

我正在尝试通过 Clear Read API 运行一堆 URL (基本上从 URL 中提取文章部分),我正在使用 AFNetworking Library .

我有一个 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 类中完成吗?

最佳答案

首先,您正在排队请求,而不是 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,您可以继续将其排入队列,它应该可以工作。

关于ios - 使用 AFNetworking 和 AFHTTPClient 通过 API 运行许多 URL - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15607358/

相关文章:

ios - 使用未解析的标识符?

ios - Audio Session 中断结束后 AVAudioRecorder 不在后台录制

iphone - 如何更改UIStatus栏颜色?

ios - 如何将标签和 UISwitch 放入自定义单元格中?我研究过,我不明白,但它类似于下图

Xcode 外部工具 cpplint

ios - Cocoapods 在 Git 克隆后缺少 project.pbxproj

ios - 使用带有水平紧凑型 UITraitCollection 的自定义 UIPresentationController,否则使用弹出窗口

ios - 在 Swift 中使用 MKLocalSearch 将一个图钉附加到用户位置,而不是蓝点

ios - iOS 中的调整大小问题

ios - AWS iOS SDK AWSServiceManager 多种服务配置