0

在我的应用程序中,我使用 json 从网络服务器获取数据。所以每次我与服务器通信时,我都会得到一个 json 并以正确的方式使用它。但有时我的服务器关闭了。那么我如何在从应用程序发送请求之前检查服务器是在线还是关闭,其他明智的应用程序会崩溃?

请帮我?

4

1 回答 1

-1

试试这个,设置一个计时器,在特定时间段后取消您的连接(与服务器的交互)

 connection = [[NSURLConnection alloc] initWithRequest: request delegate: self startImmediately:NO];

timer = [NSTimer scheduledTimerWithTimeInterval:10
                                     target:self
                                   selector:@selector(onTimeExpired)
                                   userInfo:nil
                                    repeats:NO];

[connection start];


// When time expires, cancel the connection
-(void)onTimeExpired
{
    [self.connection cancel];
}

因此,每当服务器没有响应时,json 将为空。或者即使在低速互联网连接中,json 也会被损坏,所以如果您无法与服务器连接,我建议您取消连接。

于 2013-05-18T10:41:01.497 回答