zoukankan      html  css  js  c++  java
  • 网络之AFNetworking

      AFNetsorking作为功能全面的网络第三方,既通俗好用又与时俱进-及时的更新使用了NSURLSession,不得不爱。

    AFNetsorking使用:

    1,AFNetsorking GET请求 

      ①:URL

      ②:字典存放所需的参数

    #pragma mark - get请求
    - (IBAction)getAction:(UIButton *)sender {
    
    // NSURL * url =[NSURL URLWithString:@"http://loaclhost/first.html"];
    
    AFHTTPSessionManager * manager =[AFHTTPSessionManagermanager];
    
    NSString * urlStr =@"http://localhost/logo.php";
    
    //  AFHTTPResponseSerializer * serializer
    //设置响应数据的格式为二进制(NSData)
    // manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    /** 返回的类型是后台给出的类型,设置可接受的类型 */
        manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];
    
    //设置参数
    NSDictionary * dict = @{@"userName":@"jredu",@"psw":@"123"};
    
    //网络请求方法
        [manager GET:urlStr parameters:dict progress:^(NSProgress * _Nonnull downloadProgress) {
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
    
    NSString * str = [[NSStringalloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    
    NSLog(@"成功:===%@===%@",responseObject,str);
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    NSLog(@"失败===%@",error);
    
        }];
    
    
    }

     2,AFNetsorking POST请求

    #pragma mark - POST请求
    - (IBAction)postAction:(UIButton *)sender {
    
    //请求的URL
    NSString * urlStr = @"http://localhost/login_post.php";
    
    //>初始化单例manager
    AFHTTPSessionManager * manager = [AFHTTPSessionManagermanager];
    
    //>设置接收类型
        manager.responseSerializer.acceptableContentTypes=[NSSetsetWithObject:@"text/html"];
    
    //设置参数
    NSDictionary * param = @{@"userName":@"jredu",@"psw":@"123"};
    
    //>发起网络请求
        [manager POST:urlStr parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
    
    NSLog(@"成功请求== %@",responseObject);
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    
    NSLog(@"请求失败==%@",error);
        }];
    
    }

    3,AFNetsorking 下载

     4,AFNetsorking  上传

    #pragma mark - 上传数据
    - (IBAction)upLoadAction:(UIButton *)sender {
    
    //上传PNG图片
    /*
    //    NSString * path=@"http://192.168.0.215/upload.php";
    //    
    //    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    //    
    //    [manager POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
    //        
    //        //1 将图片加载出来并转成二进制
    //        UIImage * image=[UIImage imageNamed:@"icon.png"];
    //        
    //        NSData * data=UIImagePNGRepresentation(image);
    //        
    //        //        NSData * data2=UIImageJPEGRepresentation(image, 0.5);
    //        
    //        
    //        [formData appendPartWithFileData:data name:@"file" fileName:@"zhangsan.png" mimeType:@"image/png"];
    //        
    //    } progress:^(NSProgress * _Nonnull uploadProgress) {
    //        
    //    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    //        NSLog(@"成功");
    //    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //        NSLog(@"失败");
    //    }];
        */
    
    NSString * urlStr = @"http://localhost/upload.php";
    
    AFHTTPSessionManager * manager = [AFHTTPSessionManagermanager];
    /** 设置数据为二进制(NSData) */
         manager.responseSerializer = [AFHTTPResponseSerializerserializer];
    
    //    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
        [manager POST:urlStr parameters:nilconstructingBodyWithBlock:^(id<AFMultipartFormData>_Nonnull formData) {
    
    UIImage * image = [UIImageimageNamed: @"icon.png"];
    
    //        NSData * data = UIImagePNGRepresentation(image);
    NSData * data = UIImageJPEGRepresentation(image, 0.55);
    
    //        formData =data;
            [formData appendPartWithFileData:data name:@"file"fileName:@"spiderMan.jpg"mimeType:@"image/jpeg"];
    
        } progress:^(NSProgress * _Nonnull uploadProgress) {
    
    
        } success:^(NSURLSessionDataTask * _Nonnull task, id_Nullable responseObject) {
    /** 请求网络成功 with下载的数据和请求的数据*/
    NSLog(@"seccuss");
    
    
    
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    /** 请求网络失败,返回已经下载的数据和错误信息 */
    NSLog(@"filed==%@",error);
    
        }];
    }

       的确,AFNetsorking 功能全面,使用也不复杂。但是想要其真正的为我所用,进一步的封装是必不可少的。

  • 相关阅读:
    ( 转 ) Mysql group_concat 的反向应用实现(Mysql列转行)
    ( 转 ) 优化 Group By -- MYSQL一次千万级连表查询优化
    ( 转 ) mysql复合索引、普通索引总结
    ( 转 ) 数据库BTree索引、Hash索引、Bitmap位图索引的优缺点
    ( 转 ) mysql 实战 or、in与union all 的查询效率
    ( 转 ) MySQL高级 之 explain执行计划详解
    ( 转 ) UML 类图
    ( 转 ) .net 操作 JWT
    windows 常用命令
    windows 执行bat脚本
  • 原文地址:https://www.cnblogs.com/code-Officer/p/5734576.html
Copyright © 2011-2022 走看看