zoukankan      html  css  js  c++  java
  • iOS网络-AFNetworking基本使用,文件下载,上传

    发送GET请求

    -(void)get
    {
        //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        NSDictionary *dictM = @{
                                @"username":@"520it",
                                @"pwd":@"520it",
                                @"type":@"JSON"
                                };
    
        //2.发送请求
        /*
         第一个参数:请求路径的一部分(NSString)
            以前: http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON
                 协议头+主机地址+接口名称+?+参数1&参数2
            现在: http://120.25.226.186:32812/login
                 协议头+主机地址+接口名称
         第二个参数:参数,以字典方式传递
         第三个参数:progress 进度 传递nil
         第四个参数:success 成功之后的回调
    
            task:请求Task
            responseObject:响应体
            task.response:响应头信息
         第五个参数:failure 失败之后的回调
            task:请求Task
            error:错误信息
         */
        [manager GET:@"http://120.25.226.186:32812/login" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
            NSLog(@"请求成功---%@--%@",responseObject,[responseObject class]);
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
             NSLog(@"请求失败----%@",error);
        }];
    }

    发送POST请求

    -(void)post
    {
        //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        NSDictionary *dictM = @{
                                @"username":@"520it",
                                @"pwd":@"520",
                                @"type":@"JSON"
                                };
    
        //2.发送请求
        /*
         第一个参数:请求路径的一部分(NSString)
         以前: http://120.25.226.186:32812/login?username=123&pwd=123&type=JSON
         协议头+主机地址+接口名称+?+参数1&参数2
         现在: http://120.25.226.186:32812/login
         协议头+主机地址+接口名称
         第二个参数:参数,以字典方式传递
         第三个参数:progress 进度 传递nil
         第四个参数:success 成功之后的回调
         task:请求Task
         responseObject:响应体
         task.response:响应头信息
         第五个参数:failure 失败之后的回调
         task:请求Task
         error:错误信息
         */
        [manager POST:@"http://120.25.226.186:32812/login" parameters:dictM progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
            NSLog(@"请求成功---%@--%@",responseObject,[responseObject class]);
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
            NSLog(@"请求失败----%@",error);
        }];
    }

     文件下载

    //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_01.png"]];
    
        //2.创建下载任务
        /*
         第一个参数:请求对象
         第二个参数:progress 进度回调
            downloadProgress.completedUnitCount:已经完成的大小
            downloadProgress.totalUnitCount:文件的总大小
         第三个参数:destination 自动完成文件剪切操作
            返回值:该文件应该被剪切到哪里
            targetPath:临时路径 tmp NSURL
            response:响应头
         第四个参数:completionHandler 下载完成回调
            filePath:真实路径 == 第三个参数的返回值
            error:错误信息
         */
        NSURLSessionDownloadTask *downlaodTask = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
    
            //计算文件的下载进度
            NSLog(@"%f",1.0 * downloadProgress.completedUnitCount / downloadProgress.totalUnitCount);
    
        } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
    
            //文件的全路径
            NSString *fullpath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:response.suggestedFilename];
    
            NSURL *fileUrl = [NSURL fileURLWithPath:fullpath];
    
            NSLog(@"%@
    %@",targetPath,fullpath);
            return fileUrl;
        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
    
            NSLog(@"%@",filePath);
        }];
    
        //3.执行Task
        [downlaodTask resume];

    文件上传

    -(void)uplaod
    {
        //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        NSDictionary *dictM = @{
                                @"username":@"123"
                                };
    
        //2.上传文件
        /*
         第一个参数:请求路径
         第二个参数:非文件参数,以字典传递
         第三个参数:constructingBodyWithBlock
            拼接数据 文件参数
         第四个参数:progress 进度回调
         第五个参数:success 成功回调
         第六个参数:failure 失败回调
         */
        [manager POST:@"http://120.25.226.186:32812/upload" parameters:dictM constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
            UIImage *image = [UIImage imageNamed:@"Snip20160118_860"];
            NSData *imageData = UIImagePNGRepresentation(image);
    
            /*
             第一个参数:要上传的文件的二进制数据
             第二个参数:参数名称是规定的 此处为file
             第三个参数:保存的名称
             第四个参数:mimeType 文件的数据类型
             */
            [formData appendPartWithFileData:imageData name:@"file" fileName:@"12345.png" mimeType:@"image/png"];
    
        } progress:^(NSProgress * _Nonnull uploadProgress) {
    
            NSLog(@"%f",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
            NSLog(@"请求成功---%@",responseObject);
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
              NSLog(@"请求失败---%@",error);
        }];
    }
    -(void)uplaod2
    {
        //1.创建会话管理者
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
        NSDictionary *dictM = @{
                                @"username":@"123"
                                };
    
        //2.上传文件
        /*
         第一个参数:请求路径
         第二个参数:非文件参数,以字典传递
         第三个参数:constructingBodyWithBlock
         拼接数据 文件参数
         第四个参数:progress 进度回调
         第五个参数:success 成功回调
         第六个参数:failure 失败回调
         */
        [manager POST:@"http://120.25.226.186:32812/upload" parameters:dictM constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    
            NSURL *fileUrl = [NSURL fileURLWithPath:@"/Users/xiaomage/Desktop/Snip20160118_860.png"];
    
            /*
             第一个参数:文件的URL路径
             第二个参数:参数名称是规定的 此处为file
             第三个参数:保存的名称
             第四个参数:mimeType 文件的数据类型
             */
            //[formData appendPartWithFileURL:fileUrl name:@"file" fileName:@"1.png" mimeType:@"image/png" error:nil];
    
            //简便方法 会将Snip20160118_860.png作为文件名字
            [formData appendPartWithFileURL:fileUrl name:@"file" error:nil];
    
        } progress:^(NSProgress * _Nonnull uploadProgress) {
    
            NSLog(@"%f",1.0 *uploadProgress.completedUnitCount / uploadProgress.totalUnitCount);
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    
            NSLog(@"请求成功---%@",responseObject);
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
            NSLog(@"请求失败---%@",error);
        }];
    }
  • 相关阅读:
    LeetCode 252. Meeting Rooms
    LeetCode 161. One Edit Distance
    LeetCode 156. Binary Tree Upside Down
    LeetCode 173. Binary Search Tree Iterator
    LeetCode 285. Inorder Successor in BST
    LeetCode 305. Number of Islands II
    LeetCode 272. Closest Binary Search Tree Value II
    LeetCode 270. Closest Binary Search Tree Value
    LeetCode 329. Longest Increasing Path in a Matrix
    LintCode Subtree
  • 原文地址:https://www.cnblogs.com/liugengqun/p/5140846.html
Copyright © 2011-2022 走看看