2010-02-21 210 views
5

我正在使用ASIHTTPRequest庫從我的iPhone應用程序中的服務器請求一些數據。但我不知道如何創建一個超時,以便如果服務器宕機或iPhone沒有互聯網連接的應用程序不會崩潰。超時ASIHTTP請求

在此先感謝

編輯>>>

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]); 

回答

16
[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. 
} 
+0

這會返回什麼嗎?因爲我需要有如下語句 如果(超時==真){// 某些錯誤消息 }其他{// 繼續進行下一步 } – 2010-02-21 18:10:17

+1

這只是告訴你的要求,它會在10秒後超時。如果請求超時,它會調用您的requestFailed:delegate,您可以檢查ASIRequestTimedOutError(如下面的tt.Kilew所解釋的)。 – chrissr 2010-02-21 18:13:52

+0

你可以請張貼一些示例代碼我無法得到這個工作在所有 – 2010-02-21 18:35:31

0
// Number of seconds to wait before timing out - default is 10 
NSTimeInterval timeOutSeconds; 

如果發生超時,即可免費獲贈

[self failWithError:ASIRequestTimedOutError]; 
+0

這不工作看看我發佈的示例代碼請 – 2010-02-21 18:21:47

8

我通常檢查這樣的:

- (void)requestFailed:(ASIHTTPRequest *)request 
{  
    NSString *message = NULL; 

    NSError *error = [request error]; 
    switch ([error code]) 
    { 
     case ASIRequestTimedOutErrorType: 
      message = kAlertMsgTimeoutError; 
      break; 
     case ASIConnectionFailureErrorType: 
      message = kAlertMsgConnectionFailError; 
      break; 
     case ASIAuthenticationErrorType: 
      message = kAlertMsgAuthFailError; 
      break; 
     case ASITooMuchRedirectionErrorType: 
      message = kAlertMsgTooManyRedirect; 
      break; 
     case ASIRequestCancelledErrorType: 
      message = kAlertMsgReqCancelledError; 
      break; 
     case ASIUnableToCreateRequestErrorType: 
      message = kAlertMsgUnableCreateReqError; 
      break; 
     case ASIInternalErrorWhileBuildingRequestType: 
      message = kAlertMsgUnableBuildReqError; 
      break; 
     case ASIInternalErrorWhileApplyingCredentialsType: 
      message = kAlertMsgUnableApplyCredError; 
      break; 
     case ASIFileManagementError: 
      message = kAlertMsgFileManageError; 
      break; 
     case ASIUnhandledExceptionError: 
      message = kAlertMsgUnhandledExcepError; 
      break; 
     case ASICompressionError: 
      message = kAlertMsgCompressionError; 
      break; 
     default: 
      message = kAlertMsgGenericError; 
      break; 
    } 

    if (NULL != message) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:kApplicationTitle 
                 message:message 
                 delegate:self 
               cancelButtonTitle:kAlertDefaultCancel 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 
+0

哪個class i想要爲此方法添加????? – 2013-10-19 04:35:32

+0

感謝您的支持,它效果很好。 – AndyGable 2014-01-15 21:45:06