gpt4 book ai didi

iphone - 使 ASIHTTP 请求超时

转载 作者:行者123 更新时间:2023-12-03 18:49:35 25 4
gpt4 key购买 nike

我正在使用 ASIHTTPRequest 库从 iPhone 应用程序中的服务器请求一些数据。但我不知道如何创建超时,以便在服务器出现故障或 iPhone 没有互联网连接时应用程序不会崩溃。

提前致谢

编辑>>>

tt.Kilew 你的代码不起作用...我发布了一些示例代码

NSURL *url = [NSURL URLWithString:@"A URL WITH A FORM"];
ASIFormDataRequest *requestPOST = [ASIFormDataRequest requestWithURL:url];
[requestPOST setPostValue:un forKey:@"username"];
[requestPOST setPostValue:pw forKey:@"password"];
[requestPOST setPostValue:@"Login" forKey:@"submit"];
[requestPOST start];

[requestPOST setTimeOutSeconds:10];

NSLog(@"Fail: %@", [requestPOST failWithError:ASIRequestTimedOutError]);

最佳答案

[request setTimeOutSeconds:10];

更新更多代码:

NSURL *url = [NSURL URLWithString:@"A URL WITH A FORM"];
ASIFormDataRequest *requestPOST = [ASIFormDataRequest requestWithURL:url];
[requestPOST setPostValue:un forKey:@"username"];
[requestPOST setPostValue:pw forKey:@"password"];
[requestPOST setPostValue:@"Login" forKey:@"submit"];
[requestPOST setTimeOutSeconds:10];
[requestPOST setDelegate:self]
[requestPOST startAsynchronous];

失败的处理程序:

- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
if ([error isKindOfClass:[ASIRequestTimedOutError class]]) {
// Actions specific to timeout
}
}

成功处理程序:

- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];

// Use when fetching binary data
NSData *responseData = [request responseData];

// Do something with the response.
}

关于iphone - 使 ASIHTTP 请求超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2306805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com