Flickr 搜索照片 objective-c 无 return

Flickr Search Photo objective-c nothing return

我对 Flickr API 有疑问。 我创建了 URLString

+ (NSString *)URLForSearchString:(NSString *)searchString {
NSString *APIKey = @"*****";
NSString *search = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
return [NSString stringWithFormat:@"https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=%@&tags=%@&per_page=25&format=json&nojsoncallback=1", APIKey, search];}

然后,当我触摸搜索按钮时,我使用 NSURLSession 调用了请求。

- (void)searchFlickrPhotos:(NSString *)text {
NSString *urlString = [FlickrHelper URLForSearchString:@"Nature"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString: urlString]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[UIDevice currentDevice].name forHTTPHeaderField:@"device"];
[request setTimeoutInterval:15];

NSURLSession *session;
session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];

NSURLSessionDataTask * sessionDataTask = [session dataTaskWithRequest: request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    NSDictionary *temp = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    dispatch_async(dispatch_get_main_queue(), ^{
        //For UI updates in main thread
    });
}];
[sessionDataTask resume];}

我无法从服务器获得响应。我的临时字典总是零... 你会写一些详细的解决方案吗? 非常感谢您的帮助!

为了解决这个问题,首先

  1. 检查data是否为nil
  2. 如果不为nil,则使用如下LOC在控制台查看输出

    NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    

    NSLog(@"RESPONSE: %@", str);

现在,您应该能够对实际发生的事情有另一种了解。