这个类库好久没有更新了,不过好多项目仍是在用它,因此仍是来了解一下吧。 缓存
这个第三方类库包含22个文件, 网络
1个配置, 异步
ASIHTTPRequestConfig.h 工具
1个请求代理,1个HTTP请求,1个FormData请求, url
ASIHTTPRequestDelegate.h spa
ASIHTTPRequest.h
ASIHTTPRequest.m 代理
ASIFormDataRequest.h
ASIFormDataRequest.m
1个进度代理, orm
ASIProgressDelegate.h cdn
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);
}