2016-04-01 57 views
1

我使用AFNetworking功能和我有一個關於它的問題,我想調用一個不同的功能時,返回值正確AFNetworking - 調用內部

- (AFHTTPRequestOperation *)GET:(NSString *)URLString 

        parameters:(id)parameters 

         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 

         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 

{ 

    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation 
*requestOperation, __unused id responseObject) { 

     if (success) { 

      int status = [[responseObject objectForKey:@"Status"] intValue]; 



      else if(status == -1) 

      { 

       [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) { 

        if (success) { 

         NSLog(@"adfasdfasdfasdf"); 

         success(requestOperation,responseObject); 

        } 

       } failure:^(AFHTTPRequestOperation * operation, NSError *error) { 

        NSLog(@"ERROR: %@", error); 

        failure(nil, error); 

       }]; 



      } 

      else 

       success(requestOperation,responseObject); 

     } 

    } failure:failure]; 

    operation.securityPolicy.allowInvalidCertificates = YES; 

    operation.securityPolicy.validatesDomainName = NO; 

    [self.operationQueue addOperation:operation]; 



    return operation; 

} 

當狀態= -1。我會用上面的參數調用更多的函數,但不能工作。

else if(status == -1) 

      { 

       [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) { 

        if (success) { 

         NSLog(@"adfasdfasdfasdf"); 

         success(requestOperation,responseObject); 

        } 

       } failure:^(AFHTTPRequestOperation * operation, NSError *error) { 

        NSLog(@"ERROR: %@", error); 

        failure(nil, error); 

       }]; 



      } 

運行應用程序和我的bug函數不會調用成功或失敗。

回答

0

您不需要在AFNetworking中檢查條件成功與否。 AFNetworking提供塊。

按照以下代碼:

- (AFHTTPRequestOperation *)GET:(NSString *)URLString 

       parameters:(id)parameters 

        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 

        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure 

{ 

    AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation 
*requestOperation, __unused id responseObject) { 


     int status = [[responseObject objectForKey:@"Status"] intValue]; 


     if(status == -1) 

     { 

      [self HTTPRequestOperationWithHTTPMethod:@"GET" URLString:URLString parameters:parameters success:^(AFHTTPRequestOperation *requestOperation, __unused id responseObject) { 


        NSLog(@"adfasdfasdfasdf"); 

        success(requestOperation,responseObject); 



      } failure:^(AFHTTPRequestOperation * operation, NSError *error) { 

       NSLog(@"ERROR: %@", error); 

       failure(nil, error); 

      }]; 



     } 

     else 

      success(requestOperation,responseObject); 

    } 

} failure:failure]; 

operation.securityPolicy.allowInvalidCertificates = YES; 

operation.securityPolicy.validatesDomainName = NO; 

[self.operationQueue addOperation:operation]; 



return operation; 

} 
+0

我的意思是,然後status = -1方法「[自我HTTPRequestOperationWithHTTPMethod:@」GET「URLString:URLString」dosen't工作。如何解決它。 – TrungVu

+0

如果條件成功,你首先刪除了嗎?檢查我的代碼,我已經取消了成功的條件。 – ivarun

+0

謝謝ivarun :),我完成了 – TrungVu

0
if (success) { 

     int status = [[responseObject objectForKey:@"Status"] intValue]; 



     else if(status == -1) 

     { 

你確定這是否和否則如果是一對?

+0

當然,我錯了。 – TrungVu