afnetworking 想必大家用得都比较熟了哈,自己使用 block 进行了子类化二次封装,以供参考:
+ (AFManager *)sharedManager {
static AFManager * manager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[super alloc] initWithBaseURL:[NSURL URLWithString:@"http://接口地址"]];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
});
return manager;
}
- (void)getDataFromServerWithParameters:(NSDictionary *)params completion:(SuccessBlock)completion failure:(FailureBlock)failure {
self.dicParameters = [[PostSignatureVerification sharedConstant] getSignatureVerificationDicWithParameters:params];
[self POST:@"方法名"
parameters:_dicParameters
success:^(AFHTTPRequestOperation *operation,id responseObject){
if (completion)
completion(responseObject);
} failure:^(AFHTTPRequestOperation *operation,NSError *error){
if (failure)
failure(error);
}];
}
使用的时候:
NSDictionary *parametersBaseData = @{@"id":@"*****"};
[[AFManager sharedManager] getDataFromServerWithParameters:parametersBaseData completion:^(NSDictionary *dicJson){
// 得到接口返回数据
}failure:^(NSError * error){
// 返回错误
}];