对于AFNetworking的认识我想大家都不陌生,AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持。如果项目以前使用过这些API,那么我们需要升级到基于 NSURLSession 的API的AFNetworking的版本。
以下的是针对AFNetworking3.0之前的封装,关于AFN3.0的改变我会在稍后的博客中进行介绍,毕竟多多少少还是有些人在使用AFNetwoking3.0之前的版本。
#GetData.h
首先在.h中创建三个AFHTTPRequestOperationManager对象
#import "AFHTTPRequestOperationManager.h"
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestGetManager;
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestPOSTManager;
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestHeaderPOSTManager;
同样的全局调用还是使用单例类方便一些
+ (GetData *)sharedInstance;
Get请求的方法
/**!
@brief GET 请求
@param parameters 传入的参数
@param success 调用接口成功后返回一个id类型值
@param failure 调用接口失败后返回一个错误描述
@return Dictiont
*/
- (void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
Post方法
/**!
@brief POST 请求
@param parameters 传入的参数
@param success 调用接口成功后返回一个id类型值
@param failure 调用接口失败后返回一个错误描述
*/
- (void)POSTUrl:(NSString *)url parameters:(NSDictionary *)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
上传头像的接口
/**!
@brief POST 上存头像专用接口
@param parameters 传入的参数
@param data 上存头像的文件
@param success 调用接口成功后返回一个id类型值
@param failure 调用接口失败后返回一个错误描述
*/
- (void)HeaderPOSTUrl:(NSString *)url parameters:(NSDictionary *)parameters
data:(NSData *)data
keyname:(NSString *)namestr
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
取消请求
- (void)cancelRequest;
#GetData.m
对于单例类的基本实现
1、单例对象的实现
+ (Call_Interface *)sharedInstance{
static Call_Interface *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[Call_Interface alloc]init];
});
return _sharedInstance;
}
对于Get请求方法的基本实现
- (void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
if (!_requestGetManager) {
// 设置返回格式
_requestGetManager = [AFHTTPRequestOperationManager manager];
// 设置请求格式
_requestGetManager.responseSerializer = [AFJSONResponseSerializer serializer];
}
_requestGetManager.requestSerializer.timeoutInterval = 30;
NSLog(@"%@",url);
[_requestGetManager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.cancelled == YES) {
return ;
}
failure(error);
}];
}
对于POST请求的实现
- (void)POSTUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
if (!_requestPOSTManager) {
// 设置返回格式
_requestPOSTManager = [AFHTTPRequestOperationManager manager];
// 设置请求格式
_requestPOSTManager.responseSerializer = [AFJSONResponseSerializer serializer];
}
_requestPOSTManager.requestSerializer.timeoutInterval = 30;
NSLog(@"%@",url);
[_requestPOSTManager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// if (operation.cancelled == YES) {
// return ;
// }
failure(error);
}];
// _mutaDiction = nil;
}
对于头像上传的方法实现
- (void)HeaderPOSTUrl:(NSString *)url parameters:(NSMutableDictionary *)parameters data:(NSData *)data keyname:(NSString *)namestr success:(void (^)(id))success failure:(void (^)(NSError *))failure{
if (!_requestHeaderPOSTManager) {
// 设置返回格式
_requestHeaderPOSTManager = [AFHTTPRequestOperationManager manager];
// 设置请求格式
_requestHeaderPOSTManager.responseSerializer = [AFJSONResponseSerializer serializer];
}
_requestHeaderPOSTManager.requestSerializer.timeoutInterval = 30;
NSLog(@"%@",url);
[_requestHeaderPOSTManager POST:url parameters:parameters constructingBodyWithBlock:^(id formData) {
if (data!=nil) {
int nowdate = [[NSString stringWithFormat:@"%ld",(long)[[NSDate date] timeIntervalSince1970]] intValue];
NSString * fileName = [NSString stringWithFormat:@"%d.jpg",nowdate];
[formData appendPartWithFileData:data name:namestr fileName:fileName mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.cancelled == YES) {
return ;
}
failure(error);
}];
}
取消请求
- (void)cancelRequest{
if (_requestGetManager) {
[_requestGetManager.operationQueue cancelAllOperations];
_requestGetManager = nil;
}
if (_requestHeaderPOSTManager) {
[_requestHeaderPOSTManager.operationQueue cancelAllOperations];
_requestHeaderPOSTManager = nil;
}
if (_requestPOSTManager) {
[_requestPOSTManager.operationQueue cancelAllOperations];
_requestPOSTManager = nil;
}
}
最后别忘了释放内存
- (void)dealloc{
_requestGetManager = nil;
_requestPOSTManager = nil;
_requestHeaderPOSTManager = nil;
}