1 Star 0 Fork 0

马锦航/HuoBan_160120old

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
HttpTool.m 4.70 KB
一键复制 编辑 原始数据 按行查看 历史
NewImprove 提交于 2016-01-20 15:25 . 合并后的提提交
//
// HttpTool.m
// LoveLimitFree
//
// Created by andezhou on 15/9/8.
// Copyright (c) 2015年 马锦航. All rights reserved.
//
#import "HttpTool.h"
#import "AFNetworking.h"
#import "HttpClassSelf.h"
// 服务器地址
static NSString *const kBaseURLString = @"http://api.huoban.io:8877";
//测试服务器
//static NSString *const kBaseURLString = @"http://123.57.33.133:8888";
@implementation AFHttpClient
+ (instancetype)sharedClient {
static AFHttpClient *_sharedClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedClient = [[AFHttpClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseURLString]];
_sharedClient.responseSerializer = [AFJSONResponseSerializer serializer];
_sharedClient.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", @"text/json", @"text/javascript",@"application/x-javascript",@"text/plain",@"image/gif", nil];
_sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
});
return _sharedClient;
}
@end
@implementation HttpTool
#pragma mark - AFN网络请求
#pragma mark POST请求
+ (void)postWithPath:(NSString *)path
params:(NSDictionary *)params
success:(HttpSuccessBlock)success
failure:(HttpFailureBlock)failure {
AFHttpClient *manager = [AFHttpClient sharedClient];
[manager POST:path parameters:params success:^(NSURLSessionDataTask *task, id JSON) {
if (success == nil) return;
success(JSON);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure == nil) return;
failure(error);
}];
}
#pragma mark GET请求
+ (void)getWithPath:(NSString *)path
params:(NSDictionary *)params
success:(HttpSuccessBlock)success
failure:(HttpFailureBlock)failure {
AFHttpClient *manager = [AFHttpClient sharedClient];
[manager GET:path parameters:params success:^(NSURLSessionDataTask *task, id JSON) {
if (success == nil) return;
success(JSON);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure == nil) return;
failure(error);
}];
}
#pragma mark POST上传图片
+ (void)postWithImgPath:(NSString *)path
params:(NSDictionary *)params
images:(NSArray *)images
success:(HttpSuccessBlock)success
failure:(HttpFailureBlock)failure {
NSString *basePath = [NSString stringWithFormat:@"%@%@", kBaseURLString, path];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:basePath parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (NSUInteger idx = 0; idx < images.count; idx ++) {
NSDate *date = [NSDate new];
NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyyMMddHHmmss"];
[df setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
[formData appendPartWithFileData:images[idx] name:[NSString stringWithFormat:@"header%zi", idx + 1 ] fileName:[NSString stringWithFormat:@"%@%zi.jpg", [df stringFromDate:date], idx + 1 ] mimeType:@"image/jpg"];
}
} error:nil];
AFHTTPRequestOperation *opration = [[AFHTTPRequestOperation alloc]initWithRequest:request];
opration.responseSerializer = [AFJSONResponseSerializer serializer];
opration.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[opration setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
if (success == nil) return;
success(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (failure == nil) return;
failure(error);
}];
[opration start];
}
#pragma mark delete
+ (void)deleteWithPath:(NSString *)path params:(NSDictionary *)params success:(HttpSuccessBlock)success failure:(HttpFailureBlock)failure
{
AFHttpClient *manager = [AFHttpClient sharedClient];
[manager DELETE:path parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
if (success == nil) return;
success(responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure == nil) return;
failure(error);
}];
}
#pragma mark -
#pragma mark 取消网络请求
+ (void)cancelAllRequest {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.operationQueue cancelAllOperations];
}
@end
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/2233mjh/HuoBan_160120old.git
git@gitee.com:2233mjh/HuoBan_160120old.git
2233mjh
HuoBan_160120old
HuoBan_160120old
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385