AFNetWorking的使用

1.···get请求(第一种)
- (void)_requestNetwork {
    //1. 第一个参数:接口地址
   
//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;
    }];
}
···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) {
       
    }];

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);
    }];
···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= \" %@ \" "kOAuthTokenforKey: @"Authorization"];[ request  setAllHTTPHeaderFields: headers];

AFHTTPRequestOperation  * operation  =[ AFHTTPRequestOperation  operationWithRequest: request  completion: ^( NSURLRequest  * requestNSHTTPURLResponse  * responseNSData  * dataNSError  * error){
    BOOL  HTTPStatusCodeIsAcceptable  =[[ NSIndexSet  indexSetWithIndexesInRange: NSMakeRange( 200100)]  containsIndex:[ response  statusCode]];
    if( HTTPStatusCodeIsAcceptable){
        NSLog( @"Friend Request Sent");}  else{
        NSLog( @"[Error]: (%@ %@) %@",[ request  HTTPMethod],[[ request  URLrelativePath],  error);}}];

//同时发出多个POST请求就把它加入到队列中:
NSOperationQueue  * queue  =[[[ NSOperationQueue  allocinitautorelease];[ 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) {
       
    }];
第一种:
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    [operationQueue
setMaxConcurrentOperationCount:2];
    [operationQueue addOperations:@[operation1, operation2] waitUntilFinished:NO];
第二种:
    [operation1 start];
    [operation2 start];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值