【问题标题】:AFImageRequestOperation gets "The request timed out." ErrorAFImageRequestOperation 得到“请求超时”。错误
【发布时间】:2014-03-07 08:28:54
【问题描述】:

我在从服务器下载图像数组时遇到问题,如果我在服务器上有 100 张图像,则意味着我应该使用“AFImageRequestOperation”下载所有图像,在下载过程中,一些图像已成功下载,但许多图像因“下载失败”来自服务器的 TimeOut" 错误,

我使用以下方式下载图片:

 NSURL *url = [NSURL URLWithString:kBaseURLString];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient setAuthorizationHeaderWithUsername:[[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultKeyUsername]
                                      password:[[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultKeyPassword]];

for( int i = 0; i < [self.downloadImageList count]; i++ ) {

    NSString *filename = [[NSString alloc] initWithString:[self.downloadImageList objectAtIndex:i]];
    NSMutableURLRequest *urlRequest = [httpClient multipartFormRequestWithMethod:@"POST"
                                                                            path:@"/xxx/yyyyyyyyyy/getImage"
                                                                      parameters:nil
                                                       constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {

                                                           [formData appendPartWithFormData:[filename dataUsingEncoding:NSUTF8StringEncoding]
                                                                                       name:kFormNameFile];

                                                       }];

    AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];
    [requestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

        [self updateSyncClientUIDelegateProgress:(totalBytesRead/totalBytesExpectedToRead) andLabel:@"Downloading Images"];
    }];

    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {


        if( [[urlRequest URL] isEqual:[[operation request] URL]] ) {

            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                UIImage *image = responseObject;
                NSLog(@"Downloading  image %@",image);

                [UIImagePNGRepresentation(image) writeToFile:[syncedImagesPath stringByAppendingPathComponent:filename] atomically:YES];
            });

        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        if( [[operation response] statusCode] == 404 ) {
            return;
        }
        NSLog(@"failure BLOCK %@",error);
        NSLog(@"failure error code %ld",(long)[error code]);



        if( [error code] != NSURLErrorCannotDecodeContentData ) {

            [self cancelSyncFromFailure];
        }

    }];

最后我收到一个错误(NSURLErrorDomain Code=-1001 "The request timed out.") 对于许多图像请求,

Error Report:   Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x155ef3b0 {NSErrorFailingURLStringKey=http://integration.xxx.com/yyy/zzz/getImage, NSErrorFailingURLKey=http://xxxx.yyy.com/xxx/yyyyy/getImage, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1683e130 "The request timed out."}

请帮我解决这个问题以及我应该如何解决这个问题,谢谢

【问题讨论】:

  • 嗨,我仍然被这个问题困扰,谁能帮我解决这个问题

标签: ios iphone objective-c xcode ios7


【解决方案1】:

如果您有问题尝试增加超时值,请在创建 requestOperation 之前添加此行

[urlRequest setTimeoutInterval:120]; // increase if use slow network/server 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    • 2018-06-12
    • 2011-11-26
    • 1970-01-01
    • 2021-08-08
    相关资源
    最近更新 更多