+(PJHttpTool *) defaultHttpTool{
static PJHttpTool * s_Tool = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
s_Tool = [[PJHttpTool alloc]init];
});
return s_Tool;
}
-(AFHTTPSessionManager *)sessionManager{
if (_sessionManager == nil) {
_sessionManager = [PJHttpTool createSessionManagerWithUrl:RBDom];
}
return _sessionManager;
}
+(AFHTTPSessionManager *)createSessionManagerWithUrl:(NSString *)url
{
AFHTTPSessionManager * sessionManager = [AFHTTPSessionManager manager];
sessionManager.requestSerializer.timeoutInterval = 10;
//validatesDomainName 是否需要验证域名,默认为YES;
sessionManager.securityPolicy.validatesDomainName =NO;
sessionManager.securityPolicy.allowInvalidCertificates = YES;
sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
return sessionManager;
}
+(void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id))success failure:(void (^)(NSError *))failure
{
// UInt64 recordTime = [NSDate date].timeIntervalSince1970*1000;
// NSNumber * intervalNow =[NSNumber numberWithUnsignedInteger:recordTime];
// NSLog(@"---%@--%@------",url,[NSNumber numberWithUnsignedInteger:recordTime]);
NSMutableDictionary *paramsDic = [NSMutableDictionary dictionaryWithDictionary:params];
//加入版本
// [paramsDic setObject:@"ios" forKey:@"appType"];
//
// NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
// NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
// app_build = [app_build substringFromIndex:2];
// [paramsDic setObject:app_build forKey:@"appVersion"];
// NSLog(@"%@",app_build);
// if ([[paramsDic objectForKey:@"token"] length]>0) {
// NSString * token = [NSString stringWithFormat:@"%@%@",intervalNow,[paramsDic objectForKey:@"token"]];
// NSString * aesStr = [SecurityUtil encryptAESData:token];
// [paramsDic setObject:aesStr forKey:@"token"];
// }
AFHTTPSessionManager * sessionManager = [[PJHttpTool defaultHttpTool] sessionManager];
[sessionManager POST:url parameters:paramsDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSData class]]) {
NSError * error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
responseObject = jsonObject;
}
if ( [[responseObject objectForKey:@"successFlag"] integerValue] == -1) {
[NeuProgressHUD hideHUDForView:nil];
[NeuProgressHUD showError:HttpFail toView:nil];
return ;
}
if (success)
{
if ([responseObject objectForKey:@"content"]) {
success([responseObject objectForKey:@"content"]);
}else{
success(responseObject);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
[NeuProgressHUD hideHUDForView:nil];
[NeuProgressHUD showError:HttpFail toView:nil];
failure(error);
}];
}
+ (void)postWithLoading:(NSString *)url params:(NSDictionary *)params success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure{
[NeuProgressHUD showMessage:kLoadingBestAlertText toView:nil];
[self post:url params:params success:^(id responseObj) {
[NeuProgressHUD hideHUDForView:nil];
if (success) {
success(responseObj);
}
} failure:^(NSError *error) {
if (failure) {
failure(error);
}
}];
}
#pragma mark 上传视频数据
+(void)uploadVideoWithUrl:(NSString *)url params:(NSDictionary *)params videoURL:(NSURL *)videourl progress:(void (^)(NSProgress * uploadProgress))progress success:(void (^)(id responseObj))success failure:(void (^)(NSError *error))failure{
NSData *videoData = [NSData dataWithContentsOfURL:videourl];
NSString *fileName = [videourl.absoluteString lastPathComponent];
AFHTTPSessionManager * sessionManager = [[PJHttpTool defaultHttpTool] sessionManager];
[sessionManager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:videoData name:@"uploadFile" fileName:fileName mimeType:@"video/mp4"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
progress(uploadProgress);
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if ([responseObject isKindOfClass:[NSData class]]) {
NSError * error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
responseObject = jsonObject;
}
if ( [[responseObject objectForKey:@"successFlag"] integerValue] == -1) {
[NeuProgressHUD hideHUDForView:nil];
[NeuProgressHUD showError:HttpFail toView:nil];
return ;
}
if (success)
{
if ([responseObject objectForKey:@"content"]) {
success([responseObject objectForKey:@"content"]);
}else{
success(responseObject);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
[NeuProgressHUD hideHUDForView:nil];
[NeuProgressHUD showError:HttpFail toView:nil];
failure(error);
}];
}
网络请求封装(基于AFNetworking)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- React Native中虽然也内置了XMLHttpRequest 网络请求API(也就是俗称的ajax),但XM...
- 前言 ReactiveCocoa相信大家一定不会陌生,针对于MVVM架构而已,简直是如虎添翼,用过的人自然会明白有...
- 主要内容 http get 请求 http post 请求 全局的工具类 新建一个http请求类服务 HttpSe...
- 早晨七点半,睡眼朦胧的我踩着脚踏车,赶上了十号线地铁,周二的早晨人不如周一多,但依然没有位置坐了。从起点站到这,是...