AFNetworking2.0基本使用


@interface HttpRequestManagerBase : NSObject

@property (nonatomic, strong, readonly) AFHTTPRequestOperationManager *requestManager;
@property (nonatomic, strong, readonly) AFHTTPRequestOperation *requestOperation;

#pragma mark - Asynchronous Http Request

/**
 *  所有请求通用异步的get
 *  @param: URLString 请求的URL
 *  @param: parameters 请求的参数
 *  @param: success 请求成功回调
 *  @param: failure 请求失败回调
 */
- (void)GET:(NSString *)URLString
 parameters:(id)parameters
    success:(void (^)(id responseObject))success
    failure:(void (^)(NSError *error))failure;



/**
 *  所有请求通用异步的post
 *  @param: URLString 请求的URL
 *  @param: parameters 请求的参数
 *  @param: success 请求成功回调
 *  @param: failure 请求失败回调
 */
- (void)POST:(NSString *)URLString
  parameters:(id)parameters
     success:(void (^)(id responseObject))success
     failure:(void (^)(NSError *error))failure;


@end



@interface HttpRequestManagerBase()

@property (nonatomic, strong) AFHTTPRequestOperationManager *requestManager;
@property (nonatomic, strong) AFHTTPRequestOperation *requestOperation;

@property (nonatomic, strong) dispatch_queue_t queueSerial;
@property (nonatomic, strong) dispatch_group_t groupForRequest;

@end



@implementation HttpRequestManagerBase


- (instancetype)init {
    self = [super init];
    if (self) {
        [self setupInternalManager];
        [self setupNetworkSerialQueue];
    }
    return self;
}

- (void)setupInternalManager {
    _requestManager = [AFHTTPRequestOperationManager manager];
    
}

- (void)setupNetworkSerialQueue {
    self.queueSerial = dispatch_queue_create("xxx.httpRequest.serial", DISPATCH_QUEUE_SERIAL);
    self.groupForRequest = dispatch_group_create();
}


#pragma mark - Asynchronous Http Request

/**
 * 所有请求通用异步的get
 */
- (void)GET:(NSString *)URLString
 parameters:(id)parameters
    success:(void (^)(id responseObject))success
    failure:(void (^)(NSError *error))failure
{
    dispatch_group_async(self.groupForRequest, self.queueSerial, ^{
                
        self.requestOperation = [_requestManager GET:URLString
                                          parameters:parameters
                                             success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                 
                                                 
                                                 if(responseObject && [responseObject isKindOfClass:[NSDictionary class]]){
                                                     NSDictionary *dictResponse = (NSDictionary *)responseObject;
                                                 }
                                                 
                                                 //success
                                                 if (nil != responseObject) {
                                                     
                                                     success(responseObject);
                                                 }else
                                                 {
                                                 }
                                                 
                                             } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                 
                                                 failure(error);
                                             }];
        
    });
    
    
}


/**
 * 所有请求通用异步的post
 */
- (void)POST:(NSString *)URLString
  parameters:(id)parameters
     success:(void (^)(id responseObject))success
     failure:(void (^)(NSError *error))failure
{
    dispatch_group_async(self.groupForRequest, self.queueSerial, ^{
                
        //post
        self.requestOperation = [_requestManager POST:URLString
                                           parameters:parameters
                                              success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                  //success
                                                  
                                                  if(responseObject && [responseObject isKindOfClass:[NSDictionary class]]){
                                                      NSDictionary *dictResponse = (NSDictionary *)responseObject;
                                                  }
                                                  
                                                  if (nil != responseObject) {
                                                      success(responseObject);
                                                  }else
                                                  {
                                                  }
                                                  
                                              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                  //failure                                                  
                                                  failure(error);
                                              }];
        
        
    });
    
}

AFNetworking 2.x

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"请求的url" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"成功");
} failure:^(AFHTTPRequestOperation *operation, NSError*error) {
        NSLog(@"失败");
}];

AFNetworking 3.0

AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
[session GET:@"请求的url" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"成功");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"失败");        
}];

 

你可能感兴趣的:(Objective-C)