zoukankan      html  css  js  c++  java
  • AFNetworking 使用总结

      使用步骤:

      1> 将AFNetWorking文件夹导入项目

      2> 添加 MobileCoreServices.framework、SystemConfiguration.framework 类库

      3> 在需要使用的地方导入头文件  "AFNetworking.h"

      4> 在Prefix.pch文件中import(否则会出现警告)

      #import<SystemConfiguration/SystemConfiguration.h>

      #import<MobileCoreServices/MobileCoreServices.h>

      接下来就是使用了, AFN官方建议:

      1. 定义一个全局的AFHttpClient:包含有

          1> baseURL

          2> 请求

          3> 操作队列 NSOperationQueue

       2. 由AFHTTPRequestOperation负责所有的网络操作请求

      一:用AFN加载JSON

     1   //1.baseURL
     2     NSString *baseURL = @"http://iappfree.candou.com:8080/free/applications";
     3     //2.创建AFHTTPClient
     4     AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL  URLWithString:baseURL]];
     5     //3.创建request请求
     6     NSURLRequest *request = [client requestWithMethod:@"GET" path:@"limited?currency=rmb&page=1" parameters:nil];
     7     //4.创建连接
     8     AFJSONRequestOperation *op = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
     9         //请求成功处理
    10         //直接反序列化(转成模型对象集合)
    11         NSLog(@"%@", JSON);
    12     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
    13         //请求失败处理出错处理
    14         NSLog(@"错误信息:%@", error);
    15     }];
    16     //加入队列,进行多线程操作
    17     [client.operationQueue addOperation:op];

     二、使用AFN判断网络连接状态

     1 // AFNetwork根据是否能够连接到baseUrl来判断网络连接状态的
     2     NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
     3     
     4     AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:url];
     5     /*
     6      AFNetworkReachabilityStatusUnknown          = -1,  未知
     7      AFNetworkReachabilityStatusNotReachable     = 0,   未连接
     8      AFNetworkReachabilityStatusReachableViaWWAN = 1,   3G
     9      AFNetworkReachabilityStatusReachableViaWiFi = 2,   WIFI
    10      */
    11     [client setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    12         switch (status) {
    13             case AFNetworkReachabilityStatusUnknown:
    14                 NSLog(@"未知错误");
    15                 break;
    16             case AFNetworkReachabilityStatusReachableViaWiFi:
    17                 NSLog(@"WIFI");
    18                 break;
    19             case AFNetworkReachabilityStatusReachableViaWWAN:
    20                 NSLog(@"3G网络");
    21                 break;
    22             case AFNetworkReachabilityStatusNotReachable:
    23                 NSLog(@"未连接");
    24                 break;
    25         }
    26     }];

    三、使用AFN进行下载

    //1.baseURL
        NSString *baseURL = @"http://musicdata.baidu.com";
        //2.创建AFHTTPClient
        AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL  URLWithString:baseURL]];
        
        //3.建立请求
        NSURLRequest *request = [client requestWithMethod:@"GET" path:@"data2/pic/115457125/115457125.jpg" parameters:nil];
        
        //4.操作
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        
        //5.设置下载路径(保存在沙盒中)
        
        //获取documents路径的两种方法
    #if 0
        //1>用NSSearchPathForDirectoriesInDomains
        NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documents = [docs[0] stringByAppendingPathComponent:@"Jay.jpg"];
    #elif 1
        //2>用
        NSString *documents = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/hehe.jpg"];
    #endif
        //指定输出流
        op.outputStream = [NSOutputStream outputStreamToFileAtPath:documents append:YES];
        
        //设置下载完成操作
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"下载成功");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"下载失败");
        }];
        //设置下载进度块代码
        /*
         bytesRead                      当读取的字节数
         totalBytesRead                 已经下载的字节数
         totalBytesExpectedToRead       文件总大小
         */
        [op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
            CGFloat precent = (float)totalBytesRead / totalBytesExpectedToRead;
            NSLog(@"百分比:%g", precent);
        }];
        //启动下载
        [client.operationQueue addOperation:op];
  • 相关阅读:
    scala安装使用-01
    Java基础知识强化103:Java常量池理解与总结
    C笔记01:关于printf函数输出先后顺序的讲解
    Android进阶笔记13:ListView篇之ListView刷新显示(全局 和 局部)
    Android进阶笔记12:ListView篇之图片优化
    MySQL(19):SQL语句(MySQL)大全
    Android 高级UI设计笔记09:Android实现无限滚动列表
    Android进阶笔记11:ListView篇之ListView性能优化
    1. Android 系统上一款开源的图表库
    Java基础知识强化之网络编程笔记25:Android网络通信之 Future接口介绍(Java程序执行超时)
  • 原文地址:https://www.cnblogs.com/pretty-guy/p/4067391.html
Copyright © 2011-2022 走看看