2016-10-27 43 views
0

如何再次調用相同的API我在iPhone(目標C)應用程序中使用API​​調用。我的APIS呼叫返回狀態碼200,但有一段時間它返回405響應。當我重新啓動我的應用程序時,我得到了正確的迴應 如何回調相同的API,當我得到其他然後200響應或者如果我得到錯誤...所以我不必重新啓動應用程序。如何響應其他然後狀態代碼200或錯誤(一段時間)

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

      NSString *authValue = [NSString stringWithFormat:@"Bearer %@", 
      [arrTokenData valueForKey:@"Token"]]; 

      //Configure session with common header fields 
      NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
     sessionConfiguration.HTTPAdditionalHeaders = @{@"Authorization": authValue}; 

      NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration]; 

      NSString *url = @"http://test.myserver.am/api/mobile/LookUps/getuserdata"; 
      NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 

      NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
       [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
       if (!error) { 
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
        if (httpResponse.statusCode == 200) 
        { 
         //Process the data 
        } 
        else if 
        { 
        // how to call back same API 
        } 
       } 
       else 
       { 
        // how to call back same API 
       } 

      }]; 
      [task resume]; 

在此先感謝

+0

使用遞歸函數概念。 –

+0

你應該處理你的405方法不允許的錯誤。 – Desdenova

+0

當你得到錯誤,再次調用相同的方法,這將解決你的問題 – Indrajeet

回答

-1

創建一個方法,該方法中編寫代碼,並調用同樣的方法上錯誤,如下面

- (void)callService { 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; 

    NSString *authValue = [NSString stringWithFormat:@"Bearer %@", 
          [arrTokenData valueForKey:@"Token"]]; 

    //Configure session with common header fields 
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    sessionConfiguration.HTTPAdditionalHeaders = @{@"Authorization": authValue}; 

    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration]; 

    NSString *url = @"http://test.myserver.am/api/mobile/LookUps/getuserdata"; 
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 

    NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; 
     if (!error) { 
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
      if (httpResponse.statusCode == 200) 
      { 
       //Process the data 
      } 
      else 
      { 
       // how to call back same API 
       [self callService]; 
      } 
     } 
     else 
     { 
      // how to call back same API 
      [self callService]; 
     } 

    }]; 
    [task resume]; 
} 
0

我認爲是你創建的網絡電話的一般方法。如果你會得到一個錯誤或200以外的答覆。只需從您的請求中獲取請求詳細信息,然後使用該請求(URL)調用該通用方法即可。

示例代碼:

NSURLSessionDataTask *DataTask = [session dataTaskWithRequest:theRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){ 
    NSHTTPURLResponse *serverResponse = (NSHTTPURLResponse*)response; 
    if (error == nil) { 
     if (serverResponse.statusCode == 200) { 
      NSString *theXML = [[NSString alloc] initWithBytes: 
           [data bytes] length:[data length] encoding:NSUTF8StringEncoding]; 
      NSLog(@"%@", theXML); 
     } else { 
      NSLog(@"%@, %@", serverResponse.URL, serverResponse.allHTTPHeaderFields); 
      //Call the generic network call method with the above details again 
     } 
    } else { 
     NSLog(@"%@", error. localizedDescription); 
     NSLog(@"%@, %@", serverResponse.URL, serverResponse.allHTTPHeaderFields); 
     //Call the generic network call method with the above details again 
    } 


}]; 
[DataTask resume]; 

創建新的線程,執行失敗的請求。

+0

不,我還沒有創建任何通用方法。如果我創建泛型方法,我必須保存在不同的數組中,這將很難跟蹤哪些API調用響應。我可以像1分鐘一樣再次調用相同的方法。最大1分鐘它可以調用自己,如果仍然是相同的響應我可以溝通響應用戶....像「服務器沒有響應在運動,嘗試一段時間後」東西..但我沒有任何適當的例子...因爲我第一次使用API – kiran

相關問題