gpt4 book ai didi

ios - 使用 AFNetworking 获取 OMDb API 数据

转载 作者:行者123 更新时间:2023-11-29 12:54:44 24 4
gpt4 key购买 nike

我正在尝试从 OMDb API 中获取电影数据

OMDb 提供的响应有一个错误的内容类型,但我能够规避这个问题。但是,我仍然收到来自 AFNetworking 的错误:

Error while fetching IMDBSingleMovieData: ErrorDomain=NSCocoaErrorDomain Code=3840 "The operation couldn’t becompleted. (Cocoa error 3840.)" (JSON text did not start with array orobject and option to allow fragments not set.) UserInfo=0x10a7451d0{NSDebugDescription=JSON text did not start with array or object andoption to allow fragments not set.}

代码:

// add url request parameters
NSDictionary* parameters = @{ @"i" : [NSString stringWithFormat:@"tt%d", identifier] };

// run the request operation
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:@"http://www.omdbapi.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.singleIMDBMovieData = [NSDictionary dictionaryWithDictionary:responseObject];
[self.delegate updateIMDBMovieDetails];
NSLog(@"Object returned %@", self.singleIMDBMovieData);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error while fetching IMDBSingleMovieData: %@", error);
}];

我该怎么做才能解决这个问题?来自 API 的示例响应:http://www.omdbapi.com/?i=tt0993846

最佳答案

您正在发出 POST 请求,但您想要获取数据,因此您应该使用 GET:

[manager GET:@"http://www.omdbapi.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
self.singleIMDBMovieData = [NSDictionary dictionaryWithDictionary:responseObject];
[self.delegate updateIMDBMovieDetails];
NSLog(@"Object returned %@", self.singleIMDBMovieData);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error while fetching IMDBSingleMovieData: %@", error);
}];

关于ios - 使用 AFNetworking 获取 OMDb API 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21440270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com