AFNetworking网络请求与图片上传工具(POST)
.h文件
#import <Foundation/Foundation.h> /** 成功Block */
typedef void(^SuccessBlockType) (id responsData);
/** 失败Block */
typedef void(^FaileBlockType) (NSError *error); @interface NetDataEngine : NSObject //网络请求
+ (void)postServerUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(SuccessBlockType)success failure:(FaileBlockType)failure; //上传图片
+ (void)uploadWith:(UIImage *)upImg success:(SuccessBlockType)success failure:(FaileBlockType)failure; @end
.m文件
#import "NetDataEngine.h"
#import "AFNetworking.h" @implementation NetDataEngine +(AFHTTPSessionManager *)manager
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // 声明上传的是json格式的参数,需要你和后台约定好,不然会出现后台无法获取到你上传的参数问题
manager.requestSerializer = [AFHTTPRequestSerializer serializer]; // 上传普通格式
// manager.requestSerializer = [AFJSONRequestSerializer serializer]; // 上传JSON格式 // 声明获取到的数据格式
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; // AFN不会解析,数据是data,需要自己解析
// manager.responseSerializer = [AFJSONResponseSerializer serializer]; // AFN会JSON解析返回的数据
// 个人建议还是自己解析的比较好,有时接口返回的数据不合格会报3840错误,大致是AFN无法解析返回来的数据
// 超时时间
[manager.requestSerializer willChangeValueForKey:@"timeoutInterval"]; manager.requestSerializer.timeoutInterval = 10.f;//设置请求超时时间 [manager.requestSerializer didChangeValueForKey:@"timeoutInterval"]; return manager;
} + (void)postServerUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(SuccessBlockType)success failure:(FaileBlockType)failure { AFHTTPSessionManager *manager = [self manager]; [manager POST:url parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; // JGLog(@"%@",dict);
success(dict); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
// JGLog(@"error: %@",error);
failure(error);
}];
} + (void)uploadWith:(UIImage *)upImg success:(SuccessBlockType)success failure:(FaileBlockType)failure { // 创建管理者对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager POST:UP_PIC parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { // 获取图片数据
NSData *fileData = UIImagePNGRepresentation(upImg); // 设置上传图片的名字
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str]; [formData appendPartWithFileData:fileData name:@"image" fileName:fileName mimeType:@"image/png"]; } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { //上传成功后回调
success(responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//失败回调
failure(error);
}];
} @end
如果网络请求超时,就在请求失败中执行超时操作,如:取消指示器,提示用户网络存在问题等。
if (error.code == -1001) {
[MBProgressHUD showError:@"网络超时,请检查网络"];
}
//隐藏指示器
[SVProgressHUD dismiss];