2015-04-01 70 views
0

我正在尝试使用AFHTTPRequestOperationManager发出HTTP请求。我需要使用AFHTTPRequestOperationManager,因为我希望能够在必要时取消所有操作。出于某种原因,我无法正常工作。完成块不被调用。我错过了什么吗?如何使用AFNetorking和NSURLRequest进行HTTP请求?

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/%@", username]]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setValue:@"MyUserAgent (iPhone; iOS 7.0.2; gzip)" forHTTPHeaderField:@"User-Agent"]; 
    [request setHTTPMethod:@"GET"]; 

    [self.manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSString *html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 
     if ([html containsString:@"var h = decodeURI(l.hash.substr(1)).toLowerCase();"]) { 
      completion(YES, nil); 
     } else { 
      completion(NO, nil); 
     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     completion(NO, error); 
    }]; 

回答

0

这是工作代码,您需要使用GET或POST方法。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

NSDictionary *params = @{@"email":emailfield.text}; 

[manager GET:@"http://example.com/api" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"JSON: %@", responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
}]; 
0

HTTPRequestOperationWithRequest方法返回AFHTTPRequestOperation。您必须将其添加到某个操作队列才能启动它。例如

AFHTTPRequestOperation *operation = [self.manager HTTPRequestOperationWithRequest:request ......... 
[[NSOperationQueue currentQueue] addOperation:operation]; 

您可以使用AFHTTPSessionManager比AFHTTPRequestOperationManager好一点,并使用方法的NSURLSessionDataTaskcancel取消请求。你可以在这里找到一些代码示例 - AFNetworking 2.0 cancel specific task

0

虽然其他人都是正确的 - 你应该使用现代AFNetworking结构而不是旧功能 - 有一个快速的方法来完成你想要得到的东西完成。

从外观上看,方法- (??? *) HTTPRequestOperationWithRequest:success:failure可能会返回AFHTTPRequestOperation。如果我是正确的,你只需要真正开始操作。请参阅下面的代码,更正。

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/%@", username]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setValue:@"MyUserAgent (iPhone; iOS 7.0.2; gzip)" forHTTPHeaderField:@"User-Agent"]; 
[request setHTTPMethod:@"GET"]; 

AFHTTPRequestOperation *op = [self.manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSString *html = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; 
    if ([html containsString:@"var h = decodeURI(l.hash.substr(1)).toLowerCase();"]) { 
     completion(YES, nil); 
    } else { 
     completion(NO, nil); 
    } 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    completion(NO, error); 
}]; 

[op start]; 
+0

我是想,因为我使用的是共享经理的一切,这样如果需要的话,我可以一次取消所有异步操作做这个不可思议的方式这实际上是我的应用程序非常需要的功能。但是,我只是决定让第二位经理拥有我想要的单独头域。谢谢。 – shakked 2015-04-01 22:52:50

0

入住这将工作

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil]; 
manager.requestSerializer = [AFJSONRequestSerializer serializer]; 

[manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 

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

}];