您的位置:首页 > 理论基础 > 计算机网络

ASIHTTPRequest运用

2015-12-23 18:47 651 查看
上代码:

#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_***AILABLE(10_7,
5_0);

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

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

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

-(void)uploadFiles:(LYHttpReqModel*)req cb:(void (^)(LYHttpRespModel* rep))cb cp:(void
(^)(float newProgress))cp
NS_***AILABLE(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_***AILABLE(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_***AILABLE(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_***AILABLE(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_***AILABLE(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: