HttpTool.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h> typedef void(^HttpSuccessBlock)(id json);
typedef void(^HttpFailureBlock)(NSError *error);
typedef void(^HttpDownloadProgressBlock)(CGFloat progress);
typedef void(^HttpUploadProgressBlock)(CGFloat progress); @interface HttpTool : NSObject /**
get网络请求 @param path url地址
@param params url参数 NSDictionary类型
@param success 请求成功 返回NSDictionary或NSArray
@param failure 请求失败 返回NSError
*/
+ (void)getWithPath:(NSString *)path
params:(NSDictionary *)params
success:(HttpSuccessBlock)success
failure:(HttpFailureBlock)failure; /**
post网络请求 @param path url地址
@param params url参数 NSDictionary类型
@param success 请求成功 返回NSDictionary或NSArray
@param failure 请求失败 返回NSError
*/
+ (void)postWithPath:(NSString *)path
params:(NSDictionary *)params
success:(HttpSuccessBlock)success
failure:(HttpFailureBlock)failure; /**
下载文件 @param path url路径
@param success 下载成功
@param failure 下载失败
@param progress 下载进度
*/
+ (void)downloadWithPath:(NSString *)path
success:(HttpSuccessBlock)success
failure:(HttpFailureBlock)failure
progress:(HttpDownloadProgressBlock)progress; /**
上传图片 @param path url地址
@param image UIImage对象
@param thumbName imagekey
@param params 上传参数
@param success 上传成功
@param failure 上传失败
@param progress 上传进度
*/
+ (void)uploadImageWithPath:(NSString *)path
params:(NSDictionary *)params
thumbName:(NSString *)thumbName
image:(UIImage *)image
success:(HttpSuccessBlock)success
failure:(HttpFailureBlock)failure
progress:(HttpUploadProgressBlock)progress; @end

HttpTool.m

#import "HttpTool.h"
#import "AFNetworking/AFNetworking.h" static NSString *kBaseUrl = SERVER_HOST; @interface AFHttpClient : AFHTTPSessionManager + (instancetype)sharedClient; @end @implementation AFHttpClient + (instancetype)sharedClient { static AFHttpClient *client = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; client = [[AFHttpClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseUrl] sessionConfiguration:configuration];
// 接收参数类型
client.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html", @"text/plain", @"text/gif", nil];
// 设置超时时间,默认60
client.requestSerializer.timeoutInterval = ;
// 安全策略
client.securityPolicy = [AFSecurityPolicy defaultPolicy];
}); return client;
} @end @implementation HttpTool + (void)getWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure { // 获取完整的url路径
NSString *url = [kBaseUrl stringByAppendingPathComponent:path]; [[AFHttpClient sharedClient] GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error); }];
} + (void)postWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure { // 获取完整的url路径
NSString *url = [kBaseUrl stringByAppendingPathComponent:path]; [[AFHttpClient sharedClient] POST:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(responseObject); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error); }];
} + (void)downloadWithPath:(NSString *)path success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure progress:(HttpDownloadProgressBlock)progress { // 获取完整的url路径
NSString *url = [kBaseUrl stringByAppendingPathComponent:path]; // 下载
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; NSURLSessionDownloadTask *downloadTask = [[AFHttpClient sharedClient] downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) { progress(downloadProgress.fractionCompleted); } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) { // 获取沙盒cache路径
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]]; } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) { if (error) {
failure(error);
} else {
success(filePath.path);
} }]; [downloadTask resume];
} + (void)uploadImageWithPath:(NSString *)path params:(NSDictionary *)params thumbName:(NSString *)thumbName image:(UIImage *)image success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure progress:(HttpUploadProgressBlock)progress { // 获取完整的url路径
NSString *url = [kBaseUrl stringByAppendingPathComponent:path]; NSData *data = UIImagePNGRepresentation(image); [[AFHttpClient sharedClient] POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { [formData appendPartWithFileData:data name:thumbName fileName:@"01.png" mimeType:@"image/png"]; } progress:^(NSProgress * _Nonnull uploadProgress) { progress(uploadProgress.fractionCompleted); } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { failure(error);
}];
} @end

对AFNetworking的二次封装的更多相关文章

  1. AFNetworking二次封装的那些事

    AFNetworking可是iOS网络开发的神器,大大简便了操作.不过网络可是重中之重,不能只会用AFNetworking.我觉得网络开发首先要懂基本的理论,例如tcp/ip,http协议,之后要了解 ...

  2. iOS项目相关@AFN&SDWeb的二次封装

    一,AFNetworking跟SDWebImge是功能强大且常用的第三方,然而在实际应用中需要封装用来复用今天就跟大家分享一下AFN&SDWeb的二次封装 1. HttpClient.h及.m ...

  3. iOS菜鸟之AFN的二次封装

    我用一个单例类将一些常用的网络请求进行了二次封装,主要包括post请求 get请求  图片文件上传下载  视频的断点续传等功能. 首先大家先去github上下载AFN,将文件夹内的AFNetworki ...

  4. 对百度WebUploader开源上传控件的二次封装,精简前端代码(两句代码搞定上传)

    前言 首先声明一下,我这个是对WebUploader开源上传控件的二次封装,底层还是WebUploader实现的,只是为了更简洁的使用他而已. 下面先介绍一下WebUploader 简介: WebUp ...

  5. Quick Cocos (2.2.5plus)CoinFlip解析(MenuScene display AdBar二次封装)

    转载自:http://cn.cocos2d-x.org/tutorial/show?id=1621 从Samples中找到CoinFlip文件夹,复制其中的 res 和 script 文件夹覆盖新建工 ...

  6. 对jquery的ajax进行二次封装以及ajax缓存代理组件:AjaxCache

    虽然jquery的较新的api已经很好用了, 但是在实际工作还是有做二次封装的必要,好处有:1,二次封装后的API更加简洁,更符合个人的使用习惯:2,可以对ajax操作做一些统一处理,比如追加随机数或 ...

  7. Android 应用程序集成Google 登录及二次封装

    谷歌登录API:  https://developers.google.com/identity/sign-in/android/ 1.注册并且登录google网站 https://accounts. ...

  8. Android 应用程序集成FaceBook 登录及二次封装

    1.首先在Facebook 开发者平台注册一个账号 https://developers.facebook.com/ 开发者后台  https://developers.facebook.com/ap ...

  9. 对jquery的ajax进行二次封装

    第一种方法: $(function(){ /** * ajax封装 * url 发送请求的地址 * data 发送到服务器的数据,数组存储,如:{"username": " ...

随机推荐

  1. 6、Angular Route 路由

    1.没有嵌套路由 类似 ui-route 上述的html用红字标记的是必须导入的.这是因为:路由并不在ng2中,需要我们额外引入,另外我们需要设置base href,这是个什么东西呢?相当于我们后续所 ...

  2. Checkbox与foreach循环

    呈现形态&控件语法 <span style=”display:inline-block;”> <input id=”checkBox2” type=”checkBox” na ...

  3. 02.List泛型集合

    List泛型可以转换成数组 List泛型和数组的相同点: List泛型的数据类型必须是指定的,数组的数据类型也必须是指定的. List泛型和数组的不同点: List泛型的长度是随意的,而数组的长度必须 ...

  4. glyphicons-halflings-regular.woff2 文件 404

    搜索了下,果然是因为mine没有配置的原因. http://stackoverflow.com/questions/32300578/how-to-remove-error-about-glyphic ...

  5. SQL Server日期格式化

    0   或   100   (*)     默认值   mon   dd   yyyy   hh:miAM(或   PM)       1   101   美国   mm/dd/yyyy       ...

  6. 1e6等于多少?

    如果抽象成这样:aeb 要求a不能不写,也就是说是1也要写上 b必须是整数. 实现上就是 a*10^b a乘以10的b次方 所以楼主的就是1*10^6 100000

  7. PHP连接MySQL数据库的几种方式

    PHP 5 及以上版本建议使用以下方式连接 MySQL : MySQLi :MySQLi 只针对 MySQL 数据库,MySQLi 还提供了 API 接口. PDO (PHP Data Objects ...

  8. wxpython 简单表格控件

    import wx, wx.grid class GridData(wx.grid.PyGridTableBase): _cols = "a b c".split() _data ...

  9. 【转】成型滤波与匹配滤波的MATLAB实现

    转载自:https://blog.csdn.net/yuan1164345228/article/details/45919315 Fd=1; Fs=8; Delay=3; R=0.5; [yf,tf ...

  10. selendroid之toast处理

    最近发现原来处理toast的操作失效了.仔细看了下原来的API.决定用switchTo来解决.driver.switchTo().defaultContent().findElement(By.id( ...