ios 对于AFNetworking3.0之前版本的Get、Post请求的封装(直接调用即可)

对于AFNetworking的认识我想大家都不陌生,AFNetworking在3.0版本中删除了基于 NSURLConnection API的所有支持。如果项目以前使用过这些API,那么我们需要升级到基于 NSURLSession 的API的AFNetworking的版本。

以下的是针对AFNetworking3.0之前的封装,关于AFN3.0的改变我会在稍后的博客中进行介绍,毕竟多多少少还是有些人在使用AFNetwoking3.0之前的版本。

#GetData.h

首先在.h中创建三个AFHTTPRequestOperationManager对象

#import "AFHTTPRequestOperationManager.h"

@property(nonatomic,strong)AFHTTPRequestOperationManager *requestGetManager;
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestPOSTManager;
@property(nonatomic,strong)AFHTTPRequestOperationManager *requestHeaderPOSTManager;

同样的全局调用还是使用单例类方便一些

+ (GetData *)sharedInstance;

Get请求的方法

/**!
 @brief GET 请求
 @param parameters 传入的参数
 @param success 调用接口成功后返回一个id类型值
 @param failure 调用接口失败后返回一个错误描述
 @return Dictiont
 */
- (void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters
    success:(void (^)(id responseObject))success
    failure:(void (^)(NSError *error))failure;

Post方法

/**!
 @brief POST 请求
 @param parameters 传入的参数
 @param success 调用接口成功后返回一个id类型值
 @param failure 调用接口失败后返回一个错误描述
 */
- (void)POSTUrl:(NSString *)url parameters:(NSDictionary *)parameters
     success:(void (^)(id responseObject))success
     failure:(void (^)(NSError *error))failure;

上传头像的接口

/**!
 @brief POST 上存头像专用接口
 @param parameters 传入的参数
 @param data 上存头像的文件
 @param success 调用接口成功后返回一个id类型值
 @param failure 调用接口失败后返回一个错误描述
 */
- (void)HeaderPOSTUrl:(NSString *)url parameters:(NSDictionary *)parameters
        data:(NSData *)data
        keyname:(NSString *)namestr
     success:(void (^)(id responseObject))success
     failure:(void (^)(NSError *error))failure;

取消请求

<span style="font-size:18px;">- (void)cancelRequest;</span>

#GetData.m

对于单例类的基本实现

1、单例对象的实现

+ (Call_Interface *)sharedInstance{
    static Call_Interface *_sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[Call_Interface alloc]init];
    });
    return _sharedInstance;
}
对于Get请求方法的基本实现

<span style="font-size:14px;">- (void)GETUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    if (!_requestGetManager) {
        // 设置返回格式
        _requestGetManager = [AFHTTPRequestOperationManager manager];
        // 设置请求格式
        _requestGetManager.responseSerializer = [AFJSONResponseSerializer serializer];
    }
    _requestGetManager.requestSerializer.timeoutInterval = 30;
    NSLog(@"%@",url);
    [_requestGetManager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (operation.cancelled == YES) {
            return ;
        }
        failure(error);
    }];
}
</span>

对于POST请求的实现

<span style="font-size:14px;">- (void)POSTUrl:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    if (!_requestPOSTManager) {
        // 设置返回格式
        _requestPOSTManager = [AFHTTPRequestOperationManager manager];
        // 设置请求格式
        _requestPOSTManager.responseSerializer = [AFJSONResponseSerializer serializer];
    }
    _requestPOSTManager.requestSerializer.timeoutInterval = 30;
    NSLog(@"%@",url);
    [_requestPOSTManager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        if (operation.cancelled == YES) {
//            return ;
//        }
        failure(error);
    }];
    
//    _mutaDiction = nil;
}
</span>
对于头像上传的方法实现

<span style="font-size:14px;">- (void)HeaderPOSTUrl:(NSString *)url parameters:(NSMutableDictionary *)parameters data:(NSData *)data keyname:(NSString *)namestr success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    if (!_requestHeaderPOSTManager) {
        // 设置返回格式
        _requestHeaderPOSTManager = [AFHTTPRequestOperationManager manager];
        // 设置请求格式
        _requestHeaderPOSTManager.responseSerializer = [AFJSONResponseSerializer serializer];
    }
    _requestHeaderPOSTManager.requestSerializer.timeoutInterval = 30;
    NSLog(@"%@",url);
    [_requestHeaderPOSTManager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (data!=nil) {

            int nowdate = [[NSString stringWithFormat:@"%ld",(long)[[NSDate date] timeIntervalSince1970]] intValue];
            NSString * fileName = [NSString stringWithFormat:@"%d.jpg",nowdate];

            [formData appendPartWithFileData:data name:namestr fileName:fileName mimeType:@"image/jpeg"];
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (operation.cancelled == YES) {
            return ;
        }
        failure(error);
    }];
    
}
</span>

取消请求

- (void)cancelRequest{
    if (_requestGetManager) {
        [_requestGetManager.operationQueue cancelAllOperations];
        _requestGetManager = nil;
    }
    if (_requestHeaderPOSTManager) {
        [_requestHeaderPOSTManager.operationQueue cancelAllOperations];
        _requestHeaderPOSTManager = nil;
    }
    if (_requestPOSTManager) {
        [_requestPOSTManager.operationQueue cancelAllOperations];
        _requestPOSTManager = nil;
    }
}
最后别忘了释放内存

- (void)dealloc{
    _requestGetManager = nil;
    _requestPOSTManager = nil;
    _requestHeaderPOSTManager = nil;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值