AFNetworking網絡請求與圖片上傳工具(POST)json
.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文件app
#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) {post
[MBProgressHUD showError:@"網絡超時,請檢查網絡"];url
}spa
//隱藏指示器code
[SVProgressHUD dismiss];orm