这个类库很久没有更新了,不过好多项目还是在用它,所以还是来了解一下吧。
这个第三方类库包含22个文件,
1个配置,
ASIHTTPRequestConfig.h
1个请求代理,1个HTTP请求,1个FormData请求,
ASIHTTPRequestDelegate.h
ASIHTTPRequest.h
ASIHTTPRequest.m
ASIFormDataRequest.h
ASIFormDataRequest.m
1个进度代理,
ASIProgressDelegate.h
1个缓存代理,1个缓存
ASICacheDelegate.h
ASIDownloadCache.h
ASIDownloadCache.m
2个加压解压,
ASIDataCompressor.h
ASIDataCompressor.m
ASIDataDecompressor.h
ASIDataDecompressor.m
1个输入流,
ASIInputStream.h
ASIInputStream.m
1个网络队列,
ASINetworkQueue.h
ASINetworkQueue.m
1个认证对话框,
ASIAuthenticationDialog.h
ASIAuthenticationDialog.m
1个连接工具。
Reachability.h (在源码的 External/Reachability 目录下)
Reachability.m (在源码的 External/Reachability 目录下)
依赖CFNetwork.framework,还有
SystemConfiguration.framework,
MobileCoreServices.framework,
CoreGraphics.framework,
libz.1.2.3.dylib这四个库
所以我们来看,一个网络请求的完整功能除了要解决请求功能以外,还包含配置,队列,认证,缓存,压缩解压这六个功能呢。
使用的时候#import一下头文件
#import "ASIHTTPRequest.h"
同步访问,界面卡死,一般不咋用:
- (IBAction)btnGetURLSyncClick:(UIButton *)sender {
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(@"response error:%@",response);
NSLog(@"******************************************************************");
}
}
其中用到三个对象NSURL,ASIHTTPRequest,NSError,其中用到NSURL作为参数,其实就是个字符串,NSError用来做事后检测,是否访问成功啊之类的。
主要做事的还是ASIHTTPRequest,有三个核心方法,一个是自己的初始化requestWithURL,一个启动做事的方法,startSynchronous方法,指定开始获取,一个获取回报的方法,responseString方法,用来返回获取到的结果。字符串。整个过程其实就是投入一个字符串,收获一个字符串。
异步访问,除了方法体,还得加两个回调- (IBAction)btnGetURLAsyncClick:(UIButton *)sender {
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
-(void)requestFinished:(ASIHTTPRequest *)request{
NSString *responseString = [request responseString];
NSLog(@"string:%@",responseString);
NSData *responseData = [request responseData];
NSLog(@"data:%@",responseData);
}
-(void)requestFailed:(ASIHTTPRequest *)request{
NSError *err = [request error];
NSLog(@"err:%@",err);
}
更高级的访问,多个异步访问,需要有个队列来管理进度
这个像人类的生产过程发展过程,
自己做皮鞋,
请一个工人做皮鞋,
请很多工人来做皮鞋,工人多了以后,必然要有监工来管理工人队列了,类似下面的ASINetworkQueue
ASINetworkQueue有个属性是 maxConcurrentOperationCount默认等于4,当设置为1的时候就变成串行了。。。
带队列的代码是:
static int imgCounts;
- (IBAction)btnGetURLAsyncInQueueClick:(UIButton *)sender {
NSMutableArray *imgURLArray = [[NSMutableArray alloc] initWithObjects:@"http://img03.taobaocdn.com/tps/i3/T1lh1IXClcXXajoXZd-205-130.jpg",
@"http://img02.taobaocdn.com/tps/i2/T1oN5LXvBdXXajoXZd-205-130.jpg",
@"http://img03.taobaocdn.com/tps/i3/T1FbyMXrJcXXbByyEK-755-260.jpg",
@"http://img02.taobaocdn.com/imgextra/i2/871886077/T24g65Xg8aXXXXXXXX_!!871886077.jpg_240x240.jpg",
@"http://img02.taobaocdn.com/imgextra/i2/723989220/T20AupXg4cXXXXXXXX_!!723989220.jpg_240x240.jpg",
@"http://img02.taobaocdn.com/imgextra/i2/479218086/T25T2ZXaJbXXXXXXXX_!!479218086.jpg_240x240.jpg",
@"http://img03.taobaocdn.com/imgextra/i3/228784630/T2OBD4XjFaXXXXXXXX_!!228784630.jpg_240x240.jpg",
@"http://img01.taobaocdn.com/imgextra/i1/240252102/T2.9H4Xh8aXXXXXXXX_!!240252102.jpg_240x240.jpg",
@"http://img02.taobaocdn.com/tps/i2/T1gXWBXvJhXXaDZLo7-240-160.jpg_240x240.jpg",
@"http://img04.taobaocdn.com/imgextra/i4/667336301/T2EMrwXaXbXXXXXXXX_!!667336301.jpg_240x240.jpg",
@"http://img03.taobaocdn.com/imgextra/i3/458599810/T2Xn23XfRXXXXXXXXX_!!458599810.jpg_240x240.jpg",
nil];
if(!self.queue) {
self.queue = [[ASINetworkQueue alloc] init];
self.queue.maxConcurrentOperationCount = 10;
}
for(NSString *item in imgURLArray){
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:item]];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];
[request setDidStartSelector:@selector(requestStarted:)];
[[self queue] addOperation:request];
}
[self.queue go];
}
-(void)requestStarted:(ASIHTTPRequest *)request{
//NSLog(@"---%i",self.queue.maxConcurrentOperationCount);
NSLog(@"start******%i,%@",self.queue.requestsCount,[request url]);
}
-(void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
NSLog(@"end******%i,下载到的字节数%llu,%@",imgCounts++,self.queue.bytesDownloadedSoFar ,[request url]);
}
-(void)requestWentWrong:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",error);
}