1.···get请求(第一种)
- (void)_requestNetwork {
//1.
第一个参数:接口地址
//2. 第二个参数 L :传递参数时候要用到
//GET 请求不需要传递参数,所以此处写 nil 就可以。
//POST 请求:需要传递参数,需要在接口后边拼接参数,把参数以 key : Value 的形式封装到字典里, AFN 会自动为我们把需要传递的参数拼接到我们的接口后边
//3. 第三个是网络请求成功的回调方法,就是我们需要的 JSON 数据
//2. 第二个参数 L :传递参数时候要用到
//GET 请求不需要传递参数,所以此处写 nil 就可以。
//POST 请求:需要传递参数,需要在接口后边拼接参数,把参数以 key : Value 的形式封装到字典里, AFN 会自动为我们把需要传递的参数拼接到我们的接口后边
//3. 第三个是网络请求成功的回调方法,就是我们需要的 JSON 数据
//4.
第四项是网络请求失败的时候,会回调到该方法,里边有一个
error
参数,可以通过打印
error
参数来获取错误信息。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//设置相应的格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:@"http://www.tingwen.me/akcms_category.php?id=1&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
ZMYLog(@"%@", responseObject);
for (NSDictionary *dic in responseObject) {
MessageModel *model = [MessageModel initWithDictionary:dic];
[self.datasource addObject:model];
}
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
ZMYLog(@"连接失败");
return;
}];
//设置相应的格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:@"http://www.tingwen.me/akcms_category.php?id=1&page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
ZMYLog(@"%@", responseObject);
for (NSDictionary *dic in responseObject) {
MessageModel *model = [MessageModel initWithDictionary:dic];
[self.datasource addObject:model];
}
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
ZMYLog(@"连接失败");
return;
}];
}
···get请求(第二种)
NSURL *url = [NSURL URLWithString:@"http://www.tingwen.me/akcms_category.php?id=1&page=1"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation:%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation:%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
2.POST请求(第一种)
AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSDictionary *paramter = @{@"id":@"1", @"page":@"1"};
[manage POST:@"http://www.tingwen.me/akcms_category.php?" parameters:paramter success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error:%@", error);
manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSDictionary *paramter = @{@"id":@"1", @"page":@"1"};
[manage POST:@"http://www.tingwen.me/akcms_category.php?" parameters:paramter success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error:%@", error);
}];
···POST请求(第二种)
NSMutableURLRequest
*
request
=[
NSMutableURLRequest
requestWithURL:[
NSURL
URLWithString:
@"https://gowalla.com/friendships/request?user_id=1699"]];[
request
setHTTPMethod:
@"POST"];
NSDictionary * headers =[ NSDictionary dictionaryWithObject:[ NSString stringWithFormat: @"Token token= \" %@ \" ", kOAuthToken] forKey: @"Authorization"];[ request setAllHTTPHeaderFields: headers];
AFHTTPRequestOperation * operation =[ AFHTTPRequestOperation operationWithRequest: request completion: ^( NSURLRequest * request, NSHTTPURLResponse * response, NSData * data, NSError * error){
BOOL HTTPStatusCodeIsAcceptable =[[ NSIndexSet indexSetWithIndexesInRange: NSMakeRange( 200, 100)] containsIndex:[ response statusCode]];
if( HTTPStatusCodeIsAcceptable){
NSLog( @"Friend Request Sent");} else{
NSLog( @"[Error]: (%@ %@) %@",[ request HTTPMethod],[[ request URL] relativePath], error);}}];
NSDictionary * headers =[ NSDictionary dictionaryWithObject:[ NSString stringWithFormat: @"Token token= \" %@ \" ", kOAuthToken] forKey: @"Authorization"];[ request setAllHTTPHeaderFields: headers];
AFHTTPRequestOperation * operation =[ AFHTTPRequestOperation operationWithRequest: request completion: ^( NSURLRequest * request, NSHTTPURLResponse * response, NSData * data, NSError * error){
BOOL HTTPStatusCodeIsAcceptable =[[ NSIndexSet indexSetWithIndexesInRange: NSMakeRange( 200, 100)] containsIndex:[ response statusCode]];
if( HTTPStatusCodeIsAcceptable){
NSLog( @"Friend Request Sent");} else{
NSLog( @"[Error]: (%@ %@) %@",[ request HTTPMethod],[[ request URL] relativePath], error);}}];
//同时发出多个POST请求就把它加入到队列中:
NSOperationQueue
*
queue
=[[[
NSOperationQueue
alloc]
init]
autorelease];[
queue
addOperation:
operation];
3.同时发送多个HTTP请求
AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:kURL2, 0]]]];
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation1~~~~~~~~~~%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:kURL3, 0]]]];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation2----------%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation1~~~~~~~~~~%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:kURL3, 0]]]];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"operation2----------%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
第一种:
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
[operationQueue setMaxConcurrentOperationCount:2];
[operationQueue setMaxConcurrentOperationCount:2];
[operationQueue addOperations:@[operation1, operation2] waitUntilFinished:NO];
第二种:
[operation1 start];
[operation2 start];