AFN 网络操作与AFNetworking

    xiaoxiao2021-03-26  29

    1、 AFN 特性  : * 登录传参数时 , 传递 字典 即可 .( 键名为参数名 , 键值为参数值 ). * 自动到子线程中执行 , 执行完后返 回主线程 . * 返回的结果 自动序列化 NSDictionary. 2、 使用 AFN 注意   : * AFHTTPRequestOperationManager 封装了通过 HTTP 协议与 Web 应用程序进行通讯的常用方法 . ( 这个实例化的时候 不是单例 ,  因为没有 shared ) * 包括 创建请求 / 响应序列化 / 网络监控 / 数据安全 . * 方法等都是 AF 开头的 .

    3、 AFN 能做的  ( 网络中的都涵盖了 ): * GET / POST/PUT/DELETE/HEAD 请求 . * JSON 数据解析 / Plist 数据解析 .( 不支持 XML 数据解析 ) * POST JSON . * 上传 / 下载 .

    4、 使用步骤  : (可参考说明文档) 1.首先需要实例化一个请求管理器AFHTTPRequestOperationManager. 2.设置请求的数据格式:默认是二进制.(不是可改) * AFHTTPRequestSerializer( 二进制 ) * AFJSONRequestSerializer(JSON) * AFPropertyListRequestSerializer(Plist) 3.设置响应的数据格式:默认是JSON.(不是可改) * AFHTTPResponseSerializer( 二进制 ) * AFJSONResponseSerializer(JSON) * AFPropertyListResponseSerializer(Plist) * AFXMLParserResponseSerializer(XML) * AFImageResponseSerializer(Image) * AFCompoundResponseSerializer( 组合的 ) 4.如果响应者的MIMEType不正确,就要修改acceptableContentTypes. 5.调用方法,发送响应的请求(GET/POST...).

    关于修改AFN源码:通常序列化时做对text/plan等的支持时,可以一劳永逸的修改源代码,在acceptableContentTypes中修改即可。

    AFN进行GET、POST登录:

    [objc]  view plain copy #pragma mark - get/post登录   - (void)getLogin {       //1.管理器       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];          //2.设置登录参数       NSDictionary *dict = @{ @"username":@"xn"@"password":@"123" };          //3.请求       [manager GET:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {           NSLog(@"GET --> %@, %@", responseObject, [NSThread currentThread]); //自动返回主线程       } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {           NSLog(@"%@", error);       }];   }      /**   *  和上面的GET用法完全一样, 只有一个POST参数不一样   */   - (void)postLogin {       //1.管理器       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];          //2.设置登录参数       NSDictionary *dict = @{ @"username":@"xn"@"password":@"123" };          //3.请求       [manager POST:@"http://localhost/login.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {           NSLog(@"POST --> %@, %@", responseObject, [NSThread currentThread]); //自动返回主线程       } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {           NSLog(@"%@", error);       }];   }   AFN进行网络数据解析,获取Plist,JSON,XML AFN不支持自动解析XML ,有专门的框架去做,如SAX,PULL,KissXML等)

    [objc]  view plain copy #pragma mark - get 数据解析   - (void)getJSON {       //1.请求管理器       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];          //2.发起请求       [manager GET:@"http://localhost/videos.json" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {           NSLog(@"%@", responseObject);       } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {           NSLog(@"%@", error);       }];   }      /**   *  不支持XML数据解析   */   - (void)getXML {       //1.管理器       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];          //2.设置返回数据类型       manager.responseSerializer = [AFXMLParserResponseSerializer serializer]; //先实例化一下          //3.发起请求       [manager GET:@"http://localhost/videos.xml" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {           NSLog(@"%@", responseObject);       } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {           NSLog(@"%@", error);       }];   }      - (void)getPlist {       //1.管理器       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];          //2.设置response类型       manager.responseSerializer = [AFPropertyListResponseSerializer serializer]; //是Response, 别写成request了. 修改为plist类型.       manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; //这个可以直接往框架里面修改.          //3.请求       [manager GET:@"http://localhost/videos.plist" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {           NSLog(@"%@", responseObject);       } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {           NSLog(@"%@", error);       }];   }   用AFN来POST JSON数据,上传、下载等 。(上传、下载主页说明上有 https://github.com/AFNetworking/AFNetworking

    [objc]  view plain copy #pragma mark - post json数据与上传文件等   - (void)postJSON {       //1.管理器       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];          //2.设定类型. (这里要设置request-response的类型)       manager.requestSerializer = [AFJSONRequestSerializer serializer];       manager.responseSerializer = [AFHTTPResponseSerializer serializer]; //这个决定了下面responseObject返回的类型   //    manager.responseSerializer = [AFJSONResponseSerializer serializer];   //  manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];              //2.设置登录参数       NSDictionary *dict = @{ @"username":@"xn"@"password":@"123" };          //3.发送请求       [manager POST:@"http://localhost/postjson.php" parameters:dict success: ^(AFHTTPRequestOperation *operation, id responseObject) {   //      NSLog(@"postjson--> %@", responseObject);  //这样显示JSON的话需要设置text/plain           NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];           NSLog(@"%@",result);       } failure: ^(AFHTTPRequestOperation *operation, NSError *error) {           NSLog(@"%@", error);       }];   转载自:http://blog.csdn.net/xn4545945  
    转载请注明原文地址: https://ju.6miu.com/read-664211.html

    最新回复(0)