AFNetworking使用

#import "ViewController.h"
#import 
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

get请求方式


    
      //创建AFHTTPSessionManager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    //响应体序列化对象
   // AFHTTPResponseSerializer *noSerializer = [AFHTTPResponseSerializer serializer]; //二进制的形式 (不解析)
   // AFJSONResponseSerializer *jsonSerializer = [AFJSONResponseSerializer serializer]; //json的格式 (默认)
    
    AFXMLParserResponseSerializer *xmlSerializer = [AFXMLParserResponseSerializer serializer];//xml格式
    
   // AFImageResponseSerializer *imageSerializer = [AFImageResponseSerializer serializer]; //image格式
    manager.responseSerializer = xmlSerializer;
    
    //(2)请求体序列化对象
    //请求体中的数据格式
    AFJSONRequestSerializer *jsonRequestSer =[AFJSONRequestSerializer serializer]; //json格式
    //设置请求体序列化
    manager.requestSerializer = jsonRequestSer;
    //设置get请求方式
    [manager GET:@"http://120.25.226.186:32812/login?username=520it&pwd=520it" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        NSLog(@"1%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"2%@",error);
        
    }];
    

post请求方式一

     //创建一个参数字典
    NSDictionary *dict = @{@"access_token":@"2.00ZyBPBGLWzBDDef60fc6737ygaudC",@"id":@"4008406383515844"};
    //创建AFHTTPSessionManager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //创建post申请方式
    [manager POST:@"https://api.weibo.com/2/statuses/destroy.json" parameters:dict progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //返回响应对象
        NSLog(@"-------responseObject=%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

post请求方式二

    //设置请求路径
    NSString *url = @"https://upload.api.weibo.com/2/statuses/upload.json";
    //设置参数字典
    NSDictionary *params = @{@"access_token":@"2.00ZyBPBGLWzBDDef60fc6737ygaudC",@"status":@"我们在学AFNetWorking..."};
    
    //1创建AFHTTPSessionManager对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2创建post申请方式
    [manager POST:url parameters:params constructingBodyWithBlock:^(id  _Nonnull formData) {
        
        //fromData:生成请求体的data对象
        //复杂数据 追加到formData中
        
        //获得图片路径
        NSString *path = [[NSBundle mainBundle]pathForResource:@"1.jpeg" ofType:nil];
        //获得图片data
        NSData *picData = [NSData dataWithContentsOfFile:path];
         //把图片数据追加到formData中
        [formData appendPartWithFileData:picData name:@"pic" fileName:@"a" mimeType:@"application/octet-stream"];//类型
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //上传进度
        NSLog(@"------uploadProgress%@",uploadProgress);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSLog(@"--------responseObject=%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"--------error=%@",error);
    }];


下载文件

- (void)download{
    
    AFHTTPSessionManager *mannager = [AFHTTPSessionManager manager];
    //下载路径
    NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/resources/videos/minion_01.mp4"];
       //设置路径请求
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
   
      //创建下载任务
    NSURLSessionDownloadTask *task = [mannager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
        //输出下载进度
        NSLog(@"-------downloadProgress=%@",downloadProgress);
        
    } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
        
        //targetPath:返回一个下载文件的保存路径
        //response:返回一个响应头
        
          //创建文件保存的路径
        NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/1.mp4"];
        return [NSURL fileURLWithPath:path];
        
    } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
        
        //response:返回一个响应头
        //filePath:最终保存的路径
        NSLog(@"filePath=%@",filePath);
        
        //返回错误信息(下载失败有值)
        NSLog(@"error = %@",error);
        
    }];
    
    //恢复任务
    [task resume];

你可能感兴趣的:(AFNetworking使用)