前言
AFNetworking其实实现的原理就是使用NSURLSession类,只不过使用对NSURLSession进行了封装,使得我们更加使用起来更加的方便, 其中都是通过AFHTTPSessionManager这个类来实现的!!!
1.AFHTTPSessionManager的Get请求:
NSMutableDictionary *paramter = [NSMutableDictionary dictionary];
paramter[@"q"] = @"iOS";
paramter[@"count"] = @"30";
[[AFHTTPSessionManager manager]GET:@"https://api.douban.com/v2/user" parameters:paramter
progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"responseObject:%@",responseObject);
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"error:%@",error);
}];
2.AFHTTPSessionManager的Post请求:
NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
paramer[@"q"] = @"iOS";
paramer[@"count"] = @"30";
[[AFHTTPSessionManager manager]POST:@"" parameters:paramer
progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"请求成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"请求失败");
}];
3.网络请求封装类:
NetWookTool.h
#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>
typedef void(^successBlock)(id response);
typedef void(^failureBlock)(NSError *error);
@interface NetWookTool : NSObject <NSCopying>
+(instancetype)shareNetWookTool;
-(void)GetWithUrl:(NSString *)url parameter:(id)paramter success:(successBlock)success failure:(failureBlock)failure;
-(void)PostWithUrl:(NSString *)url paramter:(id)paramter success:(successBlock)success failure:(failureBlock)failure;
@end
NetWookTool.m
#import "NetWookTool.h"
#import <AFNetworking/AFNetworking.h>
//静态的作用是防止外界访问
static NetWookTool *_tool = nil;
@implementation NetWookTool
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_tool = [super allocWithZone:zone];
});
return _tool;
}
+(instancetype)shareNetWookTool{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_tool = [[NetWookTool alloc]init];
});
return _tool;
}
-(id)copyWithZone:(NSZone *)zone{
return _tool;
}
// AFHTTPSessionManager的设置
-(AFHTTPSessionManager *)manager{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//HTTPS需要的东西了
//manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
// json的请求
manager.requestSerializer = [AFJSONRequestSerializer serializer];
// json的响应
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//xml的响应
// manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
//响应的接受的数据的格式
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/tml",@"application/json",@"text/json", nil];
return manager;
}
-(void)GetWithUrl:(NSString *)url parameter:(id)paramter success:(successBlock)success failure:(failureBlock)failure{
[[AFHTTPSessionManager manager]GET:url parameters:paramter progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (responseObject) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error) {
failure(error);
}
}];
}
-(void)PostWithUrl:(NSString *)url paramter:(id)paramter success:(successBlock)success failure:(failureBlock)failure{
[[AFHTTPSessionManager manager]POST:url parameters:paramter progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if (responseObject) {
success(responseObject);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if (error) {
failure(error);
}
}];
}
@end
结语: 这个就是我们工作中经常使用的网络请求的封装类,在我们的工作中经常使用,希望能给你带来好处!!!
网友评论