苹果Airpods pro耳机怎么改名字,苹果Airodro耳机怎么改名字?下面请看小编的详细介绍吧!......
AF3.0如何进行Get与Post请求封装
AFNetworking3.0更新针对于Get与Post请求进行二次封装,更方便进行数据请求。
工具/原料
- Xcode
- AFNetworking3.0
方法/步骤
在这里采用手动导入库的方式,将AFNetworking3.0库下载之后直接将AFNetworking3.0文件夹拖动到工程指定目录下,AFNetworking.h作为使用的全局文件,具体目录如下:
在这里将请求类封装成为单例,命名为NetWorkingManager,继承于AFHTTPSessionManager,并且声明完成度回调block和完成回调block,如下:
typedef void(^BlockProgress)(NSProgress *downloadProgress);
typedef void (^BlockFinish)(id responseObject, ResultCode resultCode, NSString *resultMessage);
进行变量枚举,声明请求状态的枚举值与请求错误状态枚举,具体如下:
get请求外部封装方法如下:
- (void )getResultWithParameter:(NSDictionary *)parameter url:(NSString *)url progress:(BlockProgress )blockProgress blockFinish:(BlockFinish )blockFinish;
parameter:参数字典。
url:请求的url。
blockProgress:完成度回调block
blockFinish:完成回调block
post请求外部封装方法如下:
- (void )postResultWithParameter:(NSDictionary *)parameter url:(NSString *)url progress:(BlockProgress )blockProgress blockFinish:(BlockFinish )blockFinish
;
parameter:参数字典。
url:请求的url。
blockProgress:完成度回调block
blockFinish:完成回调block
单例实现如下:
(NetWorkingManager *)sharedManager{
static NetWorkingManager *netWorkSingleton = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
netWorkSingleton = [[self alloc] init];
});
return netWorkSingleton;
}
请求的信息设置,在这里初始化AFHTTPSessionManager类,设置请求的Serializer与超时时间等,如下:
- (AFHTTPSessionManager *)requestBaseHttp{
AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
//header 设置
// [sessionManager.requestSerializer setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"] forHTTPHeaderField:@"appversion"];
AFJSONResponseSerializer *jsonResult = [AFJSONResponseSerializer serializer];
jsonResult.removesKeysWithNullValues = YES;
sessionManager.responseSerializer = jsonResult;
sessionManager.requestSerializer.timeoutInterval = TIMEOUT;
return sessionManager;
}
get请求实现如下:
AFHTTPSessionManager *sessionManager = [self requestBaseHttp];
[sessionManager GET:url parameters:parameter progress:^(NSProgress * _Nonnull downloadProgress) {
if (blockProgress) {
blockProgress(downloadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
ResultCode resultCode = [self checkResultCodeWithCodeString:[NSString stringWithFormat:@"%@",responseObject[@"code"]]];
NSString *resultMessage = responseObject[@"message"];
if (blockFinish) {
blockFinish(responseObject, resultCode ,resultMessage);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSString *resultMessage = [self showErroInfoWithError:error];
if (blockFinish) {
blockFinish(nil ,ResultCode_UnknowError, resultMessage);
}
}];
post请求实现如下:
AFHTTPSessionManager *sessionManager = [self requestBaseHttp];
[sessionManager POST:url parameters:parameter progress:^(NSProgress * _Nonnull uploadProgress) {
if (blockProgress) {
blockProgress(uploadProgress);
}
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
ResultCode resultCode = [self checkResultCodeWithCodeString:[NSString stringWithFormat:@"%@",responseObject[@"code"]]];
NSString *resultMessage = responseObject[@"message"];
if (blockFinish) {
blockFinish(responseObject, resultCode ,resultMessage);
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSString *resultMessage = [self showErroInfoWithError:error];
if (blockFinish) {
blockFinish(nil ,ResultCode_UnknowError, resultMessage);
}
}];
以上方法由办公区教程网编辑摘抄自百度经验可供大家参考!
相关文章