iOS 使用AFNetworking

一 下载: 网址 -- github

二 环境:

  需要引入的库 - CoreLocation.framework

  SystemConfiguration.framework

  MobileCoreServices.framework

  Security.framework

  需要在 ARC 的环境下 - 非 ARC 的工程中 - 请添加 -fobjc-arc 

三 结构:

1 : AFHTTPClient  --   提供了一个方便的网络交互接口,包括默认头,身份验证,是否连接到网络,批量处理操作,查询字符串参数序列化,已经多种表单请求

2 : AFHTTPRequestOperation -- 

和它得子类可以基于http状态和内容列下来区分是否成功请求了

3 : AFURLConnectionOperation -- 

和它的子类继承NSOperation的,允许请求被取消,暂停/恢复和由NSOperationQueue进行管理。

4 : AFURLConnectionOperation -- 

可以让你轻松得完成上传和下载,处理验证,监控上传和下载进度,控制的缓存。

四 使用 :

用法场景 1 : 请求api数据

方法1 --

创建一个 AFHTTPClient 实例

AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];

// 这里一定要有一个 baseURL - 不然会抛出一个异常 -- 

创建一个 NSURLRequest 实例

NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};

    NSURLRequest * request = [client requestWithMethod:@"POST"

                                              path:@"users/show"

                                        parameters:params];

    @param  method 网络请求方式,比如 GET,POST,PUT,DELETE , 不能为 nil -- 

    @param  path   和 baseURL 组合成为一个 url  -- 通俗的说,就是我们调用的接口 -- 比如,我想调用http://api.jiepang.com/users/show,这个接口 -- 那么 baseURL 为 http://api.jiepang.com -- path 为users/show 

    @param  parameters 网络请求参数 -- http://api.jiepang.com/v1/users/show?id=123 -- id=123 就是这个参数

创建一个 AFHTTPRequestOperation 实例进行网络链接

AFHTTPRequestOperation * operation = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {

        

        NSLog(@"success obj == %@",responseObject);

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        

        NSLog(@"faild , error == %@ ", error);

    }];

    [operation start];

    这样 - 一次简单的调用就OK了 - 

    当然也可以,用封装好的其它方法 -- 

    - (void)getPath:(NSString *)path

     parameters:(NSDictionary *)parameters

        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

    - (void)postPath:(NSString *)path

      parameters:(NSDictionary *)parameters

         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

    - (void)putPath:(NSString *)path

     parameters:(NSDictionary *)parameters

        success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

        failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

    - (void)deletePath:(NSString *)path

        parameters:(NSDictionary *)parameters

           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

- (void)patchPath:(NSString *)path

parameters:(NSDictionary *)parameters

 success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success

 failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

省去了组装 Resquest 的时间  --

比如 - 

方法二 --

    [client getPath:path

     parameters:params

        success:^(AFHTTPRequestOperation *operation, id responseObject) {

                   NSString * obj = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

                   NSLog(@"obj == %@",obj);

        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            NSLog(@"faild -- ");

        }];

    这和上面的效果是一样的 -- 

    我们看到,前面返回的数据全部为 NSdata - 为我们添加了麻烦 -- 而且我们大部分的 api 返回数据都为 json -- 

    同样 - 我们可以用 AFHTTPRequestOperation 的子类 AFJSONRequestOperation 来替代 -- 

    方法三 -- 

       NSDictionary * params = @{@"apiver": @"4",@"id" : @"830755858",@"extra_info": @"1"};

    

    

AFHTTPClient * client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://api.jiepang.com"]];

    

    NSURLRequest * request = [client requestWithMethod:@"POST"

                                                  path:@"users/show"

                                            parameters:params];

    

    

    AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

        

        NSLog(@"json == %@",JSON);

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {

        

        NSLog(@"faild -- ");

    }];

    

    [operation start];

    使用场景 2 : 异步加载图片 

    AFImageRequestOperation 是继承自 AFHTTPRequestOperation -- 所以 - 方法大同小异 -- 

    NSString * url = @"http://c.hiphotos.baidu.com/album/w=2048/sign=1d7ca85bac345982c58ae29238cc30ad/f2deb48f8c5494ee7abe33362cf5e0fe99257e04.jpg";

    // 这是一个大美女

    

    创建 request

    NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]

                                              cachePolicy:NSURLRequestReloadIgnoringLocalCacheData

                                          timeoutInterval:30];

    

    AFImageRequestOperation * operation = [AFImageRequestOperation imageRequestOperationWithRequest:request

                                                                               imageProcessingBlock:^UIImage *(UIImage *image) {

                                                                                   

                                                                                   UIImage * tempImage = [UIImage imageWithCGImage:

                                                                                                         CGImageCreateWithImageInRect(image.CGImage,ake(0, 0, image.size.width, image.size.height/2.0))];

                                                                                   

                                                                                   return tempImage;

                                                                                   

                                                                               } success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                                                                                   

                                                                                   NSLog(@"reload image success ");

                                                                                   

                                                                                   _imageView.image = image;

                                                                               } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {

                                                                                   

                                                                                   NSLog(@"reload image faild , error == %@ ",error);

                                                                                   

                                                                               }];

    

    [operation start];

    这个方法里有三个block ,success 和 failure 不说 -- processimageBlock -- 是在 图片加载完后,对 图片 处理的 block ,可以为 nil ,在 success 之前调用  --


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。