AFNetworking的使用

AFNetworking官网入门教程简单翻译,学习

AFNetworking 是一个能够快速使用的ios和mac os x下的网络框架,它是构建在Foundation URL Loading System之上的,封装了网络的抽象层,可以方便的使用,AFNetworking是一个模块化架构,拥有丰富api的框架。


一、HTTP请求与操作:
1、AFHTTPRequestOperationManager:
该类封装与Web应用程序进行通信通过HTTP,包括要求制作,响应序列化,网络可达性监控和安全性,以及要求经营管理的常见模式。

GET 请求:

  1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
  2. [manager GET:@"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  3.     NSLog(@"JSON: %@", responseObject);  
  4. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  5.     NSLog(@"Error: %@", error);  
  6. }];  


POST 带有表单参数的POST请求:

  1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
  2. NSDictionary *parameters = @{@"foo": @"bar"};  
  3. [manager POST:@"http://example.com/resources.json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  4.     NSLog(@"JSON: %@", responseObject);  
  5. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  6.     NSLog(@"Error: %@", error);  
  7. }];  


POST Multi-Part格式的表单文件上传请求:

  1. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
  2. NSDictionary *parameters = @{@"foo": @"bar"};  
  3. NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];  
  4. [manager POST:@"http://example.com/resources.json" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  
  5.     [formData appendPartWithFileURL:filePath name:@"image" error:nil];  
  6. } success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  7.     NSLog(@"Success: %@", responseObject);  
  8. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  9.     NSLog(@"Error: %@", error);  
  10. }];  


二、Session管理:
1、AFURLSessionManager:创建和管理制定的NSURLSession对象
2、NSURLSessionConfiguration对象必须实现<NSURLSessionTaskDelegate>, <NSURLSessionDataDelegate>, <NSURLSessionDownloadDelegate>, <NSURLSessionDelegate>协议


创建一个下载任务:

  1. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];  
  2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];  
  3.   
  4. NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];  
  5. NSURLRequest *request = [NSURLRequest requestWithURL:URL];  
  6.   
  7. NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];  
  8. NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithRequest:request fromFile:filePath progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {  
  9.     if (error) {  
  10.         NSLog(@"Error: %@", error);  
  11.     } else {  
  12.         NSLog(@"Success: %@ %@", response, responseObject);  
  13.     }  
  14. }];  
  15. [uploadTask resume];  


创建一个数据流任务:

  1. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];  
  2. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];  
  3.   
  4. NSURL *URL = [NSURL URLWithString:@"http://example.com/upload"];  
  5. NSURLRequest *request = [NSURLRequest requestWithURL:URL];  
  6.   
  7. NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {  
  8.     if (error) {  
  9.         NSLog(@"Error: %@", error);  
  10.     } else {  
  11.         NSLog(@"%@ %@", response, responseObject);  
  12.     }  
  13. }];  
  14. [dataTask resume];  


四、使用AFHTTPRequestOperation
1、AFHTTPRequestOperation是使用HTTP或HTTPS协议的AFURLConnectionOperation的子类。
它封装的获取后的HTTP状态和类型将决定请求的成功与否。
2、虽然AFHTTPRequestOperationManager通常是最好的去请求的方式,但是AFHTTPRequestOpersion也能够单独使用。


通过GET方式:

  1. NSURL *URL = [NSURL URLWithString:@"http://example.com/resources/123.json"];  
  2. NSURLRequest *request = [NSURLRequest requestWithURL:URL];  
  3. AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
  4. op.responseSerializer = [AFJSONResponseSerializer serializer];  
  5. [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {  
  6.     NSLog(@"JSON: %@", responseObject);  
  7. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  8.     NSLog(@"Error: %@", error);  
  9. }];  
  10. [[NSOperationQueue mainQueue] addOperation:op];  


连续操作多个:

    1. NSMutableArray *mutableOperations = [NSMutableArray array];  
    2. for (NSURL *fileURL in filesToUpload) {  
    3.     NSURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  
    4.         [formData appendPartWithFileURL:fileURL name:@"images[]" error:nil];  
    5.     }];  
    6.   
    7.     AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
    8.   
    9.     [mutableOperations addObject:operation];  
    10. }  
    11.   
    12. NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:@[...] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {  
    13.     NSLog(@"%lu of %lu complete", numberOfFinishedOperations, totalNumberOfOperations);  
    14. } completionBlock:^(NSArray *operations) {  
    15.     NSLog(@"All operations in batch complete");  
    16. }];  
    17. [[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO]; 
原文地址:https://www.cnblogs.com/zhonghuaxiaodangjia/p/3988952.html