你的位置:首页 > 操作系统

[操作系统]AFNetworking 简单应用


最近最学习 AFNetworking ,根据自己所学对 AFNetWorking 一些简单应用做了一下简单封装,主要有 get,post形式获取

 1 // 2 // AFNetWorking_Demo.h 3 // AFNetWorking_Demo 4 // 5 // Created by Ager on 15/11/4. 6 // Copyright © 2015年 Ager. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface AFNetWorking_Demo : NSObject12 13 14 /**15  * get 方法请求 16  * block success 参数类型 NS17 */18 + (void)requestvoid(^)(id void(^)(id error))fail;19 20 /**21  * post 方法请求 22  * block success 参数类型 NS23 */24 + (void)requestvoid(^)(id void(^)(id error))fail;25 26 /**27  * get 方法请求 JSON28  * block success 参数类型为解析好的 JSON 数据 , 字典或数组29 */30 + (void)requestJSON_GET:(NSString*)strUrl success:(void(^)(id json))success fail:(void(^)(id error))fail;31 32 /**33 * post 方法请求 JSON34 * block success 参数类型为解析好的 JSON 数据 , 字典或数组 35 */36 + (void)requestJSON_POST:(NSString*)strUrl parameters:(NSDictionary*)parameters success:(void(^)(id json))success fail:(void(^)(id error))fail;37 38 39 /**40  * get 方法请求 img41 */42 + (void)requestIMG_GET:(NSString*)strUrl success:(void(^)(id image))success fail:(void(^)(id error))fail;43 44 /**45  * 下载文件46  *47  * @param urlStr 要下载文件的路径48 */49 + (void)sessionDownloadWithUrl:(NSString *)urlStr success:(void (^)(NSURL *fileURL))success fail:(void (^)())fail;50 51 /**52  * 上传文件53  *54  * @param urlStr  上传网址地址55  * @param filePath 要上传的文件沙盒路径56 */57 + (void)upLoad:(NSString*)urlStr filePath:(NSString *)filePath success:(void(^)(id responseObject))success fail:(void(^)())fail;58 59 @end

AFNetWorking_Demo.h
 1 // 2 // AFNetWorking_Demo.m 3 // AFNetWorking_Demo 4 // 5 // Created by Ager on 15/11/4. 6 // Copyright © 2015年 Ager. All rights reserved. 7 // 8  9 #import "AFNetWorking_Demo.h" 10 #import "AFNetworking.h" 11  12 @implementation AFNetWorking_Demo 13  14  15 /** 16  * get 方法请求  17 */ 18 + (void)requestvoid(^)(id void(^)(id error))fail{ 19    20   AFHTTPRequestOperationManager *mamager = [AFHTTPRequestOperationManager manager]; 21    22   mamager.responseSerializer = [[AF 23    24   [mamager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 25      26     success(responseObject); 27      28   } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 29      30     fail(error); 31      32   }]; 33    34 } 35  36  37  38 /** 39  * post 方法请求  40  * 41  * parameters 为 JSON 数据类型 42 */ 43 + (void)requestvoid(^)(id void(^)(id error))fail{ 44    45   AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager]; 46   //设置请求格式 47   maneger.requestSerializer = [[AFJSONRequestSerializer alloc]init]; 48   //设置返回数据格式 49   maneger.responseSerializer = [[AF 50    51    52   [maneger POST:strUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 53     success(responseObject); 54   } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 55     fail(error); 56   }]; 57 } 58  59 /** 60  * get 方法请求 json 61 */ 62 + (void)requestJSON_GET:(NSString*)strUrl success:(void(^)(id json))success fail:(void(^)(id error))fail{ 63    64   AFHTTPRequestOperationManager *mamager = [AFHTTPRequestOperationManager manager]; 65    66   mamager.responseSerializer = [[AFJSONResponseSerializer alloc]init]; 67    68   mamager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",@"text/html",nil]; 69    70   [mamager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 71      72     success(responseObject); 73      74   } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 75      76     fail(error); 77      78   }]; 79    80 } 81  82  83  84  85 + (void)requestIMG_GET:(NSString*)strUrl success:(void(^)(id image))success fail:(void(^)(id error))fail{ 86   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 87    88   manager.responseSerializer = [[AFImageResponseSerializer alloc]init]; 89    90   [manager GET:strUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 91     if (success) { 92       success(responseObject); 93     } 94   } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 95     if (fail) { 96       fail(error); 97     } 98   }]; 99 }100 101 /**102  * 文件下载103 */104 + (void)sessionDownloadWithUrl:(NSString *)urlStr success:(void (^)(NSURL *fileURL))success fail:(void (^)())fail105 {106   107   AFURLSessionManager *manage = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];108   109   //设置 request110   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];111    //设置下载任务112   NSURLSessionDownloadTask *tast = [manage downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {113     //指定文件下载路径114     //将下载文件保存在缓存路径中115     116     NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];117     NSString *filePath = [path stringByAppendingString:response.suggestedFilename];118     119     // URLWithString返回的是网络的URL,如果使用本地URL,需要注意120     NSURL *url = [NSURL fileURLWithPath:filePath];121     122     if (success) {123       success(url);124     }125     return url;126     127   } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {128     NSLog(@"%@---- %@", filePath, error);129     if (error) {130       fail();131     }132   }];133   134   [tast resume];135 }136 137 /**138  * 文件上传139  * block success 参数类型为解析好的 JSON 数据 , 字典或数组 140 */141 + (void)upLoad:(NSString*)urlStr filePath:(NSString *)filePath success:(void(^)(id responseObject))success fail:(void(^)())fail{142   143   AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];144   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];145   146   NSURLSessionUploadTask *task = [manager uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:filePath] progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {147     if (error) {148       fail();149     }else{150       success(responseObject);151     }152   }];153   [task resume];154   155   156 }157 158 /**159  * POST - 请求JSON160  * 需要导入 UIKit+AFNetworking161 */162 + (void)requestJSON_POST:(NSString*)strUrl parameters:(NSDictionary*)parameters success:(void(^)(id json))success fail:(void(^)(id error))fail{163   164   AFHTTPRequestOperationManager *maneger = [AFHTTPRequestOperationManager manager];165   //设置请求格式166   maneger.requestSerializer = [[AFHTTPRequestSerializer alloc]init];167   //设置返回数据格式168   maneger.responseSerializer = [[AFJSONResponseSerializer alloc]init];169  170   maneger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/plain",@"text/html",nil];171   172   [maneger POST:strUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {173     success(responseObject);174   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {175     fail(error);176   }];177 }178 179 180 @end

AFNetWorking_Demo.m

AFNetworking-2.6.1.zip 下载