zoukankan      html  css  js  c++  java
  • iOS之AFN框架基本使用(2)

    AFN框架基本使用

    1)AFN内部结构

    AFN结构体

        - NSURLConnection

            + AFURLConnectionOperation(已经被废弃)

            + AFHTTPRequestOperation(已经被废弃)

            + AFHTTPRequestOperationManager(封装了常用的 HTTP 方法)(已经被废弃)

                * 属性

                    * baseURL :AFN建议开发者针对 AFHTTPRequestOperationManager 自定义个一个单例子类,设置 baseURL, 所有的网络访问,都只使用相对路径即可

                    * requestSerializer :请求数据格式/默认是二进制的 HTTP

                    * responseSerializer :响应的数据格式/默认是 JSON 格式

                    * operationQueue

                    * reachabilityManager :网络连接管理器

                * 方法

                    * manager :方便创建管理器的类方法

                    * HTTPRequestOperationWithRequest :在访问服务器时,如果要告诉服务器一些附加信息,都需要在 Request 中设置

                    * GET

                    * POST

        - NSURLSession

            + AFURLSessionManager

            + AFHTTPSessionManager(封装了常用的 HTTP 方法)

                * GET

                * POST

                * UIKit + AFNetworking 分类

                * NSProgress :利用KVO

        - 半自动的序列化&反序列化的功能

            + AFURLRequestSerialization :请求的数据格式/默认是二进制的

            + AFURLResponseSerialization :响应的数据格式/默认是JSON格式

        - 附加功能

            + 安全策略

                * HTTPS

                * AFSecurityPolicy

            + 网络检测

                * 对苹果的网络连接检测做了一个封装

                * AFNetworkReachabilityManager

    建议:

    可以学习下AFN对 UIKit 做了一些分类, 对自己能力提升是非常有帮助的

    2)AFN的基本使用

    (1)发送POST请求的方式

    -(void)post

    {

        //1.创建会话管理者

        //AFHTTPSessionManager内部是基于NSURLSession实现的

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

        //2.创建参数

        NSDictionary *dict = @{

                               @"username":@"520it",

                               @"pwd":@"520it",

                               };

        //3.发送POST请求

        /*

         http://120.25.226.186:32812/login?username=ee&pwd=ee&type=JSON

         第一个参数:NSString类型的请求路径,AFN内部会自动将该路径包装为一个url并创建请求对象

         第二个参数:请求参数,以字典的方式传递,AFN内部会判断当前是POST请求还是GET请求,以选择直接拼接还是转换为NSData放到请求体中传递

         第三个参数:进度回调 此处为nil

         第四个参数:请求成功之后回调Block

         第五个参数:请求失败回调Block

         */

        [manager POST:@"http://120.25.226.186:32812/login" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            //注意:responseObject:请求成功返回的响应结果(AFN内部已经把响应体转换为OC对象,通常是字典或数组)

            NSLog(@"请求成功---%@",responseObject);

        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

            NSLog(@"请求失败---%@",error);

        }];

    }

    (2)使用AFN下载文件

    -(void)download

    {

        //1.创建会话管理者

        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

        //2.创建请求对象

        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://120.25.226.186:32812/resources/images/minion_13.png"]];

        //3.创建下载Task

        /*

         第一个参数:请求对象

         第二个参数:进度回调

            downloadProgress.completedUnitCount :已经下载的数据

            downloadProgress.totalUnitCount:数据的总大小

         第三个参数:destination回调,该block需要返回值(NSURL类型),告诉系统应该把文件剪切到什么地方

            targetPath:文件的临时保存路径

            response:响应头信息

         第四个参数:completionHandler请求完成后回调

            response:响应头信息

            filePath:文件的保存路径,即destination回调的返回值

            error:错误信息

         */

        NSURLSessionDownloadTask *downloadTask = [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];

            NSLog(@"%@ %@",targetPath,fullPath);

            return [NSURL fileURLWithPath:fullPath];

        } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {

            NSLog(@"%@",filePath);

        }];

        //4.执行Task

        [downloadTask resume];

    }

  • 相关阅读:
    [轉]Linux kernel <2.6.29 exit_notify() local root exploit分析(2009-1337)
    [轉]udp_sendmsg空指针漏洞分析 by wzt
    linux 中mmap的用法
    [轉]Exploit The Linux Kernel NULL Pointer Dereference
    [轉]Exploit Linux Kernel Slub Overflow
    Linux 2.6.x fs/pipe.c local kernel root(kit?) exploit (x86)
    字符串哈希专题
    树形DP
    ACM中的正则表达式
    回文树学习笔记
  • 原文地址:https://www.cnblogs.com/chenjianjian/p/5387782.html
Copyright © 2011-2022 走看看