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
#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
相关文章推荐
- IOS开发网络篇之──ASIHTTPRequest下载示例(支持断点续传)
- KJHttp的基础功能和工作原理
- 面试总结8--计算机网络相关问题Part2
- iOS 获取网络状态
- android4.0 HttpClient 以后不能在主线程发起网络请求
- java最简单的方式实现httpget和httppost请求
- 卷积神经网络基础
- 网络编程之IO复用:select or epoll
- jquery sortable的拖动方法内容说明和示例详解(转载http://www.jb51.net/article/45803.htm)
- jquery sortable的拖动方法内容说明和示例详解(转载http://www.jb51.net/article/45803.htm)
- TCP和UDP的区别有哪些?
- ASIHTTPRequest 上传文件无响应问题研究
- ASIHTTPRequest 上传文件无响应问题研究
- 批处理、分时、实时、网络、分布式操作系统的区别
- libnids中TCP/IP栈实现细节分析(上)——TCP会话重组
- Quartz实现动态定时任务--http://my.oschina.net/u/1177710/blog/284608
- Network Analysis]复杂网络分析总结
- TCP异常终止(reset报文)
- 解决iOS9新特性无法使用http的解决方法
- bzoj1305[CQOI2009]dance跳舞