0

我正在开发一个 iPhone 应用程序,我在其中通过 web-services 从另一台服务器加载大量数据。

我想在苹果指南中的某处阅读,对于网络感知应用程序,您必须设置网络超时,然后您会提醒用户“网络不可用”。

我该怎么做?

4

1 回答 1

2

这是调用 Web 服务的示例代码,请求超时 = 20。如果它在 20 时间内没有响应,那么它将停止连接,我们得到一个 nil 数据。

NSString* str = [NSString stringWithFormat:@"http://ws.geonames.org/findNearbyPostalCodes?lat=%f&lng=%f",curr_latitude,curr_longitude];
NSMutableURLRequest* request2=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:str]];
[request2 setHTTPMethod:@"POST"];   
[request2 setTimeoutInterval:20];
NSURLResponse *response=nil;
NSError *err=nil;
NSData *data1=[[NSURLConnection sendSynchronousRequest:request2 returningResponse:&response error:&err] retain];
if(data1 == nil)
{
    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"The network is not available.\n Please check the Internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];

}
  else
  {
         // It will store all data to data1
         // Here you can proceed with data1
  }
于 2009-10-13T10:45:13.393 回答