AFNetworking 使用系列之 ~ 2.5.4 版本的一些封装
- 使用block
typedef void(^RequestCompletionBlock) (id result, BOOL succeed);
#import <AFNetworking.h>
- GET 请求
+ (void)commonRequestByGetWithURL:(NSString *)url parameters:(NSDictionary *)parameters completion:(RequestCompletionBlock)completion
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer=[AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer.timeoutInterval = 30;
[manager GET:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
completion(responseObject,YES);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
completion(error,NO);
}];
}
- POST请求
+ (void)commonRequestByPostWithURL:(NSString *)url parameters:(NSDictionary *)parameters completion:(RequestCompletionBlock)completion
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer=[AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager.requestSerializer setValue:@"text/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
manager.requestSerializer.timeoutInterval = 30;
[manager POST:url parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
completion(responseObject,YES);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
completion(error,NO);
}];
}
-上传图片
+ (void)commonUploadWithUrl:(NSString *)url image:(NSData *)imageData imageName:(NSString *)imageName completion:(RequestCompletionBlock)completion
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSString *newName = [NSString stringWithFormat:@"%@%@",imageName,@".png"];
// 上传图片
[formData appendPartWithFileData:imageData name:@"portrait" fileName:newName mimeType:@""];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
completion(responseObject,YES);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
completion(error,NO);
}];
}
-下载
+ (void)commonDownloadWithUrl:(NSString *)url filePath:(NSURL *)filePath completion:(RequestCompletionBlock)completion
{
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
// NSURL *URL = [NSURL URLWithString:@"http://example.com/download.zip"];
NSURL *URL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
if (error) {
completion(filePath,NO);
}else{
completion(filePath,YES);
}
}];
[downloadTask resume];
}
参考