代码拉取完成,页面将自动刷新
//
// 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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。