【问题标题】:Unsupported URL Error after successful call成功调用后不支持的 URL 错误
【发布时间】:2015-08-25 16:22:15
【问题描述】:

我很困惑为什么即使图像已成功下载并显示,错误块仍在调用。我在日志中得到的错误是,

Error = Error Domain=NSURLErrorDomain Code=-1002 "不支持的 URL" UserInfo=0x7fe7840d8000 {NSUnderlyingError=0x7fe781d5b5e0 "不支持的 URL", NSErrorFailingURLStringKey=, NSErrorFailingURLKey=, NSLocalizedDescription=不支持的 URL}

即使我收到此错误,我也可以获取图像,对其进行解析并将其显示在应用程序中。这段代码我哪里错了,

@try{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSURL *url = [NSURL URLWithString:imageUrl];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *res, NSData *data, NSError *error){
 UIImage *thumbImage = nil;
 if ([data length] >0 && error == nil){
    NSLog(@"image was downloaded.");
    thumbImage = [UIImage imageWithData:data];
 }
 else if ([data length] == 0 && error == nil){
    NSLog(@"Nothing was downloaded.");
    [loaderView removeFromSuperview];
 }
 else if (error != nil){
    NSLog(@"Error = %@", error);
    [loaderView removeFromSuperview];
 }
  dispatch_async(dispatch_get_main_queue(), ^{
    imageViewtoApply.image = thumbImage;
    [webServiceDelegate imageDownloadFinished];
    [loaderView removeFromSuperview];
    });
   }];
 });
}

【问题讨论】:

    标签: ios dispatch-async


    【解决方案1】:

    查看http状态码,可能是错误码。

    将响应投射到 NSHTTPURLResponse 并检查 statusCode 属性。

    NSHTTPURLResponse *URLResponse = (NSHTTPURLResponse *)res;
    NSLog(@"http status code: res.statusCode", %i);
    

    【讨论】:

    • 状态码为“0”。如果为 0,如何下载和显示图像?
    • 真的吗?见Status Code Definitions,0 不是其中之一。您可以随时使用Charles Proxy 查看请求和响应。
    猜你喜欢
    • 2015-05-11
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 2017-08-03
    • 2012-07-07
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多