ASIHTTPRequest运用

上代码:

#import <Foundation/Foundation.h>

#import "LYHttpReqModel.h"

#import "LYHttpRespModel.h"

#import "ASIHTTPRequestDelegate.h"

#import "ASIHTTPRequest.h"


typedef void(^onLYReqCallBack)(LYHttpRespModel* rep);

typedef void(^onLYReqDownLoadProgress)(float newProgress);

//onLYReqCallBack callBack;

//onLYReqDownLoadProgress callProgress;


//  该类为http异步请求类,每次需要异步请求则需要alloc一个该类的对象

//  如果使用单例模式,那么在delegate返回数据后,很难区分数据是多个请求中的哪个的

@interface LYAsynHttpReq : NSObject <ASIHTTPRequestDelegate, ASIProgressDelegate>


@property (nonatomic, strong, readwrite) onLYReqCallBack callBack;


@property (nonatomic, strong, readwrite) onLYReqDownLoadProgress callProgress;


-(void)jsonReq:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb NS_AVAILABLE(10_7, 5_0);

-(void)downLoadFile:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb cp:(void (^)(float newProgress))cp NS_AVAILABLE(10_7, 5_0);

//  主要用于文件上传等表单提交

-(void)formReq:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb NS_AVAILABLE(10_7, 5_0);

-(void)uploadFiles:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb cp:(void (^)(float newProgress))cp NS_AVAILABLE(10_7, 5_0);

//  在回调函数中调用,避免该对象引用为0

- (void)destroy;

@end



#import "LYAsynHttpReq.h"

#import "TFUtils.h"

#import "ASIDownloadCache.h"

#import "ASIFormDataRequest.h"


实现:

@implementation LYAsynHttpReq

@synthesize callBack, callProgress;


-(void)jsonReq:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb NS_AVAILABLE(10_7, 5_0)

{

    if(callBack == nil){

        callBack = cb;

        NSURL *url = [NSURL URLWithString:req.url];

        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];

        request.delegate = self;

        [request addRequestHeader:@"Content-Type" value:@"text/xml; charset=utf-8"];

        [request addRequestHeader:@"Accept" value:@"text/xml"];

        [request setPostBody:[TFUtils dicToMutData:req.postData]];

        //[request setPostBody:[TFUtils stingToNSData:req.postDataStr]];

        //[request setPostValue:@"123" forKey:@"pwd"];

        //[request setPostBody:];

        if([TFUtils isNull:req.method]){

            [request setRequestMethod:@"GET"];

        }else{

            [request setRequestMethod:req.method];

        }

        if(req.headers != nil){

            for(NSString* key in req.headers){

                [request addRequestHeader:key value:[req.headers valueForKey:key]];

            }

        }

        if(![TFUtils isNull:req.downloadFilePath]){

            [request setDownloadDestinationPath:req.downloadFilePath];

        }

        if(req.useCache){

            [request setDownloadCache:[ASIDownloadCache sharedCache]];

        }

        [request startAsynchronous];

    }else{

        DLog(@"do not request twice in one ly req");

    }

}


-(void)downLoadFile:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb cp:(void (^)(float newProgress))cp NS_AVAILABLE(10_7, 5_0)

{

    if(callProgress == nil){

        callProgress = cp;

        [self jsonReq:req cb:cb];

    }else{

        DLog(@"do not request twice in one ly req");

    }

}


-(void)formReq:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb NS_AVAILABLE(10_7, 5_0)

{

    if(callBack == nil){

        DLog(@"form req");

        callBack = cb;

        NSURL *url = [NSURL URLWithString:req.url];

        ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];

        request.delegate = self;

        if([TFUtils isNull:req.method]){

            [request setRequestMethod:@"GET"];

        }else{

            [request setRequestMethod:req.method];

        }

        if(req.headers != nil){

            for(NSString* key in req.headers){

                [request addRequestHeader:key value:[req.headers valueForKey:key]];

            }

        }

        if(req.postData != nil){

            for(NSString* key in req.postData){

                [request setPostValue:[req.postData valueForKey:key] forKey:key];

            }

        }

        if(req.files != nil){

            DLog(@"------------------filepath");

            for(NSString* key in req.files){

                DLog(@"---------111");

                [request addFile:[req.files valueForKey:key] forKey:key];

                DLog(@"-------------%@",key);

            }

        }

        if(![TFUtils isNull:req.downloadFilePath]){

            [request setDownloadDestinationPath:req.downloadFilePath];

        }

        if(req.useCache){

            [request setDownloadCache:[ASIDownloadCache sharedCache]];

        }

        [request startAsynchronous];

    }else{

        DLog(@"do not request twice in one ly req");

    }

}


-(void)uploadFiles:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb cp:(void (^)(float newProgress))cp NS_AVAILABLE(10_7, 5_0)

{

    if(callProgress == nil){

        callProgress = cp;

        [self formReq:req cb:cb];

    }else{

        DLog(@"do not request twice in one ly req");

    }

}

// 请求成功

- (void)requestFinished:(ASIHTTPRequest *)request

{

    DLog(@"req success");

    LYHttpRespModel* lyResp = [[LYHttpRespModel alloc] init];

    lyResp.respStatus = [request responseStatusCode];

    if(lyResp.respStatus == 200){

        DLog(@"status :200");

        // Use when fetching text data

        lyResp.respText = [request responseString];

        DLog(@"resptext: %@",lyResp.respText);

        id jsonResult = [TFUtils stringToJsonObj:lyResp.respText];

        if([jsonResult isKindOfClass:[NSDictionary class]]){

            lyResp.respJson = jsonResult;

        }else if([jsonResult isKindOfClass:[NSArray class]]){

            lyResp.respJsonArr = jsonResult;

        }

        // Use when fetching binary data

        lyResp.respData = [request responseData];

    }else{

        lyResp.respError = [[NSError alloc] init];

    }

    if(callBack){

        DLog(@"send callback");

        callBack(lyResp);

    }else{

        DLog(@"request callback is nil");

    }

}

// 请求失败

- (void)requestFailed:(ASIHTTPRequest *)request

{

    DLog(@"req failed");

    LYHttpRespModel* lyResp = [[LYHttpRespModel alloc] init];

    lyResp.respStatus = [request responseStatusCode];

    lyResp.respError = [request error];

    if(callBack){

        callBack(lyResp);

    }else{

        DLog(@"request callback is nil");

    }

}

// 上传或下载进度回调

- (void)setProgress:(float)newProgress

{

    if(callProgress){

        callProgress(newProgress);

    }

}


- (void)request:(ASIHTTPRequest *)request willRedirectToURL:(NSURL *)newURL

{


}


- (void)destroy

{


}


@end




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值