需要使用CocoaPods集成ReactiveCocoa/ReactiveObjc和AFNetWorking
或直接下载我写的示例工程https://github.com/Geniune/LJAPI
创建AFHTTPSessionManager的Category,并import ReactiveObjC(建议写在PrefixHeader内),封装原本dataTaskWithHTTPMethod:方法转换为RACSignal
- (RACSignal *)signalForRequest:(NSString *)url method:(NSString *)method parameters:(NSDictionary *)params {
return [[RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {//创建一个RACSignal
NSURLSessionDataTask *dataTask = [self dataTaskWithHTTPMethod:method URLString:url parameters:params uploadProgress:^(NSProgress *uploadProgress) {
} downloadProgress:^(NSProgress *downloadProgress) {
} success:^(NSURLSessionDataTask *task, id responseObject) {
[subscriber sendNext:responseObject];//成功回调
[subscriber sendCompleted];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[subscriber sendError:error];//失败回调
[subscriber sendCompleted];
}];
[dataTask resume];
return [RACDisposable disposableWithBlock:^{
//当信号销毁,请求也取消
[dataTask cancel];
}];
}] replayLazily];
}
创建BaseHttpClient类,实现:
- AFHTTPSessionManager对象初始化
- requestSerializer和responseSerializer属性设置
- 取消上一个/全部请求 方法
- 若使用HTTPS,SSL证书对应securityPolicy设置
- (void)requestURL:(NSString *)url params:(NSDictionary *)params method:(NSString *)method success:(void(^)(id responseObj))success failure:(void(^)(NSError *error))failure{
//发起一个HTTP请求
[[_http signalForRequest:url method:method parameters:params] subscribeNext:^(id _Nullable x) {
if(success){
success(x);
}
} error:^(NSError * _Nullable error) {
if (failure) {
failure(error);
}
}];
}
创建HttpClient单例类,实现:
- BaseURL拼接 方法,当然也可以设置AFHTTPSessionManager的baseURL属性
- 入参params的加密 算法
- Get、Post、Put、Delete等具体 方法
- (void)requestWithPath:(NSString *)path params:(NSDictionary *)params method:(NSString *)method success:(void(^)(id responseObj))success failure:(void(^)(HttpException * e))failure{
[self requestURL:path params:params method:method success:^(id responseObj) {
NSInteger status = [[responseObj objectForKey:@"status"] integerValue];
if(status == 200){
//说明业务逻辑正常
if(success){
success(responseObj[@"data"]);
}
}else{
//业务逻辑异常处理
HttpException *e = [HttpException new];
e.errorCode = status;
e.message = [responseObj objectForKey:@"message"];
if (failure) {
failure(e);
}
}
} failure:^(NSError *error) {
HttpException *e = [HttpException new];
e.errorCode = error.code;
e.message = error.description;
if (failure) {
failure(e);
}
}];
}
- (void)get:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObj))success failure:(void(^)(HttpException * e))failure{
[self requestWithPath:url params:params method:@"GET" success:^(id responseObj) {
if(success){
success (responseObj);
}
} failure:^(HttpException *e) {
if(failure){
failure(e);
}
}];
}
- (void)post:(NSString *)url params:(NSDictionary *)params success:(void(^)(id responseObj))success failure:(void(^)(HttpException * e))failure{
[self requestWithPath:url params:params method:@"POST" success:^(id responseObj) {
if(success){
success (responseObj);
}
} failure:^(HttpException *e) {
if(failure){
failure(e);
}
}];
}
创建一个异步POST请求:
NSString *API = @"https://www.example.com/?action=UserInfo";//接口URL
NSDictionary *param = @{@"user_id":@"9802"};//入参数据
[HttpClientMgr post:API params:param success:^(id responseObj) {
//成功回调
} failure:^(HttpException *e) {
//失败回调
}];
下篇将说明如何封装一个简单的业务API
链接:iOS开发-网络-合理封装请求接口(二)业务API接入实现
网友评论