我正在使用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;
}
当status = -1时。我将用上面的param调用更多函数,但是不能正常工作。
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 :(得分: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;
}
答案 1 :(得分:0)
if (success) {
int status = [[responseObject objectForKey:@"Status"] intValue];
else if(status == -1)
{
你确定这个,如果是一对吗?