【问题标题】:How to handle network or server error? [closed]如何处理网络或服务器错误? [关闭]
【发布时间】:2012-09-21 12:45:24
【问题描述】:

我正在使用以下代码

NSMutableArray *image=[[NSMutableArray alloc]init];
    for(int i=1;i<4;i++)
    {
        NSString *urlString =[NSString stringWithFormat:@"http://www.isco.com/webproductimages/appBnr/bnr%d.jpg",i];
        NSData *photoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
       [image addObject:photoData];
    }

我的问题是,如果发生网络故障,它会显示线程... 这个线程怎么处理??

【问题讨论】:

    标签: iphone objective-c ios xcode ipad


    【解决方案1】:

    您的“线程”问题(这是一个错误/异常)可能是由多种因素引起的。可能是互联网连接中断,或者服务器抛出某种形式的 http 错误。

    首先,请确保您在运行网络操作时不会丢失任何互联网连接。关于如何检查互联网连接有很多建议。一种是使用SDK本身提供的Reachability类。看看这篇文章如何做到这一点:How to check for an active Internet connection on iOS or OSX?

    本文还将为您提供有关互联网连接的更多见解:Testing internet connection on iPad app using ios5

    当您确定不是 Internet 连接,而是 http 服务器错误时。确保您正在调用的 http 服务正确地传回数据。我使用一些 REST 客户端工具来检查这个 (REST Client)

    最后,我在浏览器上测试了您尝试调用的 URL,它会引发 Bad Request 错误。这意味着它与您的 iOS 代码无关。如果此服务器在您的控制之下,您应该修复它。

    【讨论】:

      【解决方案2】:
          NSMutableArray *image=[[NSMutableArray alloc]init];
              for(int i=1;i<4;i++)
              {
                  NSString *urlString =[NSString stringWithFormat:@"http://www.isco.com/webproductimages/appBnr/bnr%d.jpg",i];
      
               NSError *error;
           NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString] options:NSDataReadingUncached error:&error    ];
      
              if (data) {
            [image addObject:photoData];
               }
              else{
                  if (error) {
                      NSLog(error);
                  }
              }
      
      
        }
      

      【讨论】:

        猜你喜欢
        • 2016-07-11
        • 1970-01-01
        • 2010-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-19
        • 2017-08-05
        相关资源
        最近更新 更多