0

我正在访问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 发出请求?

4

1 回答 1

1

您应该检查错误对象包含的内容,但我认为您的基本 url 不正确,因为它包含 get 参数的键,但在需要时会从 AFNetworking 添加这些键。

sharedClient = [[AFClearReadClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.thequeue.org/v1/clear?url=&format="]];

应该读

sharedClient = [[AFClearReadClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.thequeue.org/v1/clear"]]

检查错误,做

…
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Request operation error %@", [error localizedDescription]);
}];

您也可以尝试将基本网址设置http://api.thequeue.org/

NSMutableURLRequest *request = [[AFClearReadClient sharedClient] requestWithMethod:@"GET" 
                                              path: [NSString stringWithFormat:@"/v1/clear?url=%@&format=json", articleURL]
                                        parameters:nil];

如果这碰巧起作用而另一个不起作用,我认为这是因为 api 文档中规定的限制:

重要提示:url 查询必须始终排在第一位。


我重新创建了您的项目。Indead 您的第一个代码会产生这个格式错误的网址:

 NSErrorFailingURLKey=http://api.thequeue.org/v1/clear/?url=&format=?format=json&url=http%3A%2F%2Fwww.nytimes.com%2F2013%2F03%2F25%2Fbusiness%2Fglobal%2Fcyprus-and-europe-officials-agree-on-outlines-of-a-bailout.html%3Fhp%26_r%3D0

并通过我的修复:

http://api.thequeue.org/v1/clear/?format=json&url=http%3A%2F%2Fwww.nytimes.com%2F2013%2F03%2F25%2Fbusiness%2Fglobal%2Fcyprus-and-europe-officials-agree-on-outlines-of-a-bailout.html

什么在技术上是正确的,但是 api 是有限的,因为 url 参数不是第一个。

于 2013-03-25T15:01:59.240 回答