2016-10-27 6 views
0

私のiPhone(目的C)アプリケーションでAPI呼び出しを使用しています。私のAPISコールはステータスコード200を返しますが、しばらくして405の応答を返します。私は私のアプリを再起動するとき私は適切な応答を取得します。 他の200の応答を受け取った場合や、エラーが発生した場合に同じAPIをコールバックする方法...アプリを再起動する必要はありません。応答が他の場合に同じAPIを再度呼び出す方法ステータスコード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

関連する問題