用Block封装ASIHttpRequest

Posted 刚刚888

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用Block封装ASIHttpRequest相关的知识,希望对你有一定的参考价值。

用Block封装ASIHttpRequest

横方便的网络请求方法,不用每次都写代理方法.使代码先得很整齐,不会凌乱.
接口部分:
//
//  LYHASIRequestBlock.h
//  ASIBlockTest
//
//  Created by Charles Leo on 14-7-23.
//  Copyright (c) 2014年 Charles Leo. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
typedef void (^RequestBlock) (void);
@interface LYHASIRequestBlock : NSObject <ASIHTTPRequestDelegate>
{
    ASIHTTPRequest * getRequest;
    ASIFormDataRequest * postRequest;
    NSURL * url;
    RequestBlock finishBlock;
    RequestBlock failBlock;
    RequestBlock startBlock;
    NSString * requestType;
}

//接收到的数据
@property (strong,nonatomic) NSData * receiveData;
//请求完成的block
- (void)didFinishBlock:(RequestBlock)block;
//请求失败的block
- (void)didFailedBlock:(RequestBlock)block;
//请求开始的block
- (void)didStartBlock:(RequestBlock)block;
//取消请求
- (void)cancelRequst;
//get请求方法
- (void)getRequest:(NSString *)getUrl;
//post请求方法
- (void)postRequest:(NSString *)postUrl andKeys:(NSArray *)keyArray andValues:(NSArray *)valueArray;
@end

实现部分:
//
//  LYHASIRequestBlock.m
//  ASIBlockTest
//
//  Created by Charles Leo on 14-7-23.
//  Copyright (c) 2014年 Charles Leo. All rights reserved.
//

#import "LYHASIRequestBlock.h"

@implementation LYHASIRequestBlock

- (void)getRequest:(NSString *)getUrl
{
    requestType = @"GET";
    getRequest  = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:getUrl]];
    getRequest.delegate = self;
    getRequest.timeOutSeconds = 15;
    [getRequest startAsynchronous];
}
#pragma mark -GET请求的代理方法
//开始请求
- (void)requestStarted:(ASIHTTPRequest *)request
{
    startBlock();
}
//请求完成
- (void)requestFinished:(ASIHTTPRequest *)request
{
    self.receiveData = request.responseData;
    finishBlock();
}
//请求失败
- (void)requestFailed:(ASIHTTPRequest *)request
{
    failBlock();
}
//post请求
-(void)postRequest:(NSString *)postUrl andKeys:(NSArray *)keyArray andValues:(NSArray *)valueArray
{
    postRequest = [[ASIFormDataRequest alloc]initWithURL:[NSURL URLWithString:postUrl]];
    postRequest.timeOutSeconds = 15;
    postRequest.delegate = self;
    for (int i = 0; i<keyArray.count; i++) {
        [postRequest setPostValue:[valueArray objectAtIndex:i] forKey:[keyArray objectAtIndex:i]];
    }
    [postRequest setDidFinishSelector:@selector(didFinishPostRequest:)];
    [postRequest setDidStartSelector:@selector(didStartPostRequest:)];
    [postRequest setDidFailSelector:@selector(didFailPostRequest:)];
    [postRequest startAsynchronous];
}

#pragma mark - POST请求的代理方法
//请求开始
- (void)didStartPostRequest:(ASIFormDataRequest *)request
{
    startBlock();
}
//请求完成
- (void)didFinishPostRequest:(ASIFormDataRequest *)request
{
    finishBlock();
}
//请求失败
- (void)didFailPostRequest:(ASIFormDataRequest *)request
{
    failBlock();
}

//设置Blocks
//设置开始块
- (void)didStartBlock:(RequestBlock)block
{
    [startBlock release];
    startBlock = [block copy];
}
//设置完成块
-(void)didFinishBlock:(RequestBlock)block
{
    [finishBlock release];
    finishBlock = [block copy];
}
//设置失败块
-(void)didFailedBlock:(RequestBlock)block
{
    [failBlock release];
    failBlock = [block copy];
}
//取消请求
- (void)cancelRequst{
    if ([requestType isEqualToString:@"GET"])
    {
        [getRequest cancel];
    }
    else if([requestType isEqualToString:@"POST"])
    {
        [postRequest cancel];
    }
}

-(void)dealloc
{
    [getRequest release];
    [postRequest release];
    [startBlock release];
    [finishBlock release];
    [failBlock release];
    [super dealloc];
}
@end

 

以上是关于用Block封装ASIHttpRequest的主要内容,如果未能解决你的问题,请参考以下文章

ASIHTTPRequest类库简介和使用说明

IOS-网络(ASIHTTPRequest的使用简介)

IOS开发 REST请求 ASIHTTPRequest用法

NSOperationQueue 和 ASIHTTPRequest

Block

iOS开发面试拿offer攻略之block篇