我正在访问Clear Read API以从链接中提取文章文本,并且 API 通过将参数放入此 URL 来工作:http://api.thequeue.org/v1/clear?url=&format=
在 URL 之后可以放置的位置,例如,http://www.nytimes.com/2013/03/25/business/global/cyprus-and-europe-officials-agree-on-outlines-of-a-bailout.html?hp&_r=0
在格式之后放置json
.
然后,它将以状态代码、an 的形式返回 JSON,item
然后在item
文章的标题、其 URL 和提取的文章文本中返回。
我正在尝试通过 AFNetworking 和 AFHTTPClient(子类为 AFClearReadClient)与以下代码进行交互:
我的 AFClearReadClient 类:
#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;
}
以及我的根视图控制器中的以下内容:
- (void)addArticlesToQueueFromList:(NSDictionary *)articles {
// Restrict amount of operations that can occur at once
[[AFClearReadClient sharedClient].operationQueue setMaxConcurrentOperationCount:5];
// Create an array to hold all of our requests to make
NSMutableArray *requestOperations = [[NSMutableArray alloc] init];
for (NSString *key in articles) {
// Create the request from the article's URL and the request parameters
NSString *articleURL = [[articles objectForKey:key] objectForKey:@"resolved_url"];
NSDictionary *requestParameters = @{@"url": articleURL,
@"format": @"json"};
NSMutableURLRequest *request = [[AFClearReadClient sharedClient] requestWithMethod:@"GET" path:nil parameters:requestParameters];
// Create the request operation and specify behaviour on success and failure
AFHTTPRequestOperation *requestOperation = [[AFClearReadClient sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
// Get the item NSDictionary from the JSON responseObject
NSDictionary *item = [responseObject objectForKey:@"item"];
// Get the values needed to create an article
NSString *title = [item objectForKey:@"title"];
NSString *URL = [item objectForKey:@"link"];
NSString *body = [item objectForKey:@"description"];
// Create and add the article to our list of articles
Article *article = [[Article alloc] initWithTitle:title URL:URL body:body];
[self.articles insertObject:article atIndex:0];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request operation error");
}];
// Save the request operation in an NSArray so all can be enqueued later
[requestOperations addObject:requestOperation];
}
// Enqueue the request operations
[[AFClearReadClient sharedClient] enqueueBatchOfHTTPRequestOperations:requestOperations progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"Processing...");
[self.tableView reloadData];
} completionBlock:^(NSArray *operations) {
NSLog(@"Done!");
}];
}
但是每当我运行它时,我都希望我的表格视图被填充(它从 RootViewController 的文章数组中获取它的单元格),但是我在控制台中得到了以下内容:
2013-03-25 11:31:35.470 [19020:c07] Processing...
2013-03-25 11:31:35.471 [19020:c07] Request operation error
2013-03-25 11:31:35.476 [19020:c07] Processing...
2013-03-25 11:31:35.476 [19020:c07] Request operation error
2013-03-25 11:31:35.477 [19020:c07] Processing...
2013-03-25 11:31:35.477 [19020:c07] Request operation error
2013-03-25 11:31:35.480 [19020:c07] Processing...
2013-03-25 11:31:35.480 [19020:c07] Request operation error
2013-03-25 11:31:35.481 [19020:c07] Processing...
2013-03-25 11:31:35.482 [19020:c07] Request operation error
2013-03-25 11:31:35.488 [19020:c07] Done!
到底出了什么问题?我已经考虑过了,但我似乎无法弄清楚出了什么问题。当我一次只使用一个 NSURLConnection 时(对于我想做的事情来说效率不够高),它起作用了,但我似乎把 AFNetworking 搞砸了。
它与我的requestParameters
变量有关吗?我是否错误地向 Clear Read API 发出请求?