這個類庫好久沒有更新了,不過好多項目仍是在用它,因此仍是來了解一下吧。 緩存
這個第三方類庫包含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);
}