對于AFNetworking的認識我想大家都不陌生谴垫,AFNetworking在3.0版本中刪除了基于 NSURLConnection API的所有支持蜡镶。如果項目以前使用過這些API,那么我們需要升級到基于 NSURLSession 的API的AFNetworking的版本。
以下的是針對AFNetworking3.0之前的封裝械巡,關(guān)于AFN3.0的改變我會在稍后的博客中進行介紹刹淌,畢竟多多少少還是有些人在使用AFNetwoking3.0之前的版本。
#GetData.h
首先在.h中創(chuàng)建三個AFHTTPRequestOperationManager對象
#import "AFHTTPRequestOperationManager.h"
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestGetManager;
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestPOSTManager;
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestHeaderPOSTManager;
同樣的全局調(diào)用還是使用單例類方便一些
+ (GetData *)sharedInstance;
Get請求的方法
/**!
@brief GET 請求
@param parameters 傳入的參數(shù)
@param success 調(diào)用接口成功后返回一個id類型值
@param failure 調(diào)用接口失敗后返回一個錯誤描述
@return Dictiont
*/
- (void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
Post方法
/**!
@brief POST 請求
@param parameters 傳入的參數(shù)
@param success 調(diào)用接口成功后返回一個id類型值
@param failure 調(diào)用接口失敗后返回一個錯誤描述
*/
- (void)POSTUrl:(NSString *)url parameters:(NSDictionary *)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure;
上傳頭像的接口
/**!
@brief POST 上存頭像專用接口
@param parameters 傳入的參數(shù)
@param data 上存頭像的文件
@param success 調(diào)用接口成功后返回一個id類型值
@param failure 調(diào)用接口失敗后返回一個錯誤描述
*/
- (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
對于單例類的基本實現(xiàn)
1讥耗、單例對象的實現(xiàn)
+ (Call_Interface *)sharedInstance{
static Call_Interface *_sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedInstance = [[Call_Interface alloc]init];
});
return _sharedInstance;
}
對于Get請求方法的基本實現(xiàn)
- (void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
if (!_requestGetManager) {
// 設(shè)置返回格式
_requestGetManager = [AFHTTPRequestOperationManager manager];
// 設(shè)置請求格式
_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請求的實現(xiàn)
- (void)POSTUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
if (!_requestPOSTManager) {
// 設(shè)置返回格式
_requestPOSTManager = [AFHTTPRequestOperationManager manager];
// 設(shè)置請求格式
_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;
}
對于頭像上傳的方法實現(xiàn)
- (void)HeaderPOSTUrl:(NSString *)url parameters:(NSMutableDictionary *)parameters data:(NSData *)data keyname:(NSString *)namestr success:(void (^)(id))success failure:(void (^)(NSError *))failure{
if (!_requestHeaderPOSTManager) {
// 設(shè)置返回格式
_requestHeaderPOSTManager = [AFHTTPRequestOperationManager manager];
// 設(shè)置請求格式
_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;
}
}
最后別忘了釋放內(nèi)存
- (void)dealloc{
_requestGetManager = nil;
_requestPOSTManager = nil;
_requestHeaderPOSTManager = nil;
}