为什么我的JSON请求失败了AFNetworking?

时间:2013-03-25 14:35:06

标签: ios objective-c cocoa-touch afnetworking

我正在访问Clear Read API以从链接中提取文章文本,并且API通过在此网址中添加参数来运行:http://api.thequeue.org/v1/clear?url=&format=您可以在网址之后放置http://www.nytimes.com/2013/03/25/business/global/cyprus-and-europe-officials-agree-on-outlines-of-a-bailout.html?hp&_r=0json 1}}并且格式化后你会放item

然后它会以状态代码item的形式返回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; } 文章标题,其网址和提取的文章文本中返回。

我正尝试通过AFNetworking和AFHTTPClient(子类为AFClearReadClient)与以下代码进行交互:

我的AFClearReadClient类:

- (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!");
    }];
}

以及我的根视图控制器中的以下内容:

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!

但每当我运行它时,我都希望我的表视图被填充(它从RootViewController的文章数组中获取它的单元格),但我在控制台中得到以下内容:

requestParameters

到底出了什么问题?我已经仔细研究了这个,但我似乎无法弄清楚出了什么问题。当我一次只使用一个NSURLConnection时(对我想做的事情来说还不够高效)它起作用了,但我似乎搞乱了AFNetworking。

是否与我的{{1}}变量有关?我是否错误地向Clear Read API发出请求?

1 个答案:

答案 0 :(得分:1)

您应该检查错误对象包含的内容,但我认为您的基本网址不对,因为它包含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文件中规定的限制:

  

重要:网址查询必须始终排在第一位。


我重新创建了你的项目。合并你的frist代码会产生这个格式错误的网址:

 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参数不是第一个。