標籤:
從本篇開始,阿堂準備進一步介紹ASIHTTPRequest架構下載資料和上傳資料的實際應用。
為了實現多線程並發請求網路能力,ASIHTTPRequest被設計成 NSOperation的子類。ASINetworkQueue被設計成NSOpertaionQueue的子類。如果NSOpertaionQueue是線程管理器,NSOperation就相當於一個線程。它們被添加到NSOperationQueue隊列中有序執行。ASINetworkQueue 和ASIHTTPRequest也有同樣的概念,只是ASINetworkQueue線程管理器提供更多的與網路相關的服務,如獲得下載進度等。所以,下面阿堂就主要介紹ASINetworkQueue管理請求隊列的demo了。 下面,請隨阿堂看一個例子介紹隊列的用法。當我們點擊go按鈕時,從伺服器端同時下載兩張圖片並將它們顯示在介面中。
其中兩個檔案的完整原始碼如下ViewController.h, ViewController.m
#import
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
#import "NSNumber+Message.h"
#import "NSString+URLEncoding.h"
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UIImageView *imageView2;
@property (strong) ASINetworkQueue *networkQueue;
- (IBAction)onClick:(id)sender;
@end
---------------------------------------------------------
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (IBAction)onClick:(id)sender {
if (!_networkQueue) {
_networkQueue = [[ASINetworkQueue alloc] init];
}
// 停止以前的隊列
[_networkQueue cancelAllOperations];
// 建立ASI隊列
[_networkQueue setDelegate:self];
[_networkQueue setRequestDidFinishSelector:@selector(requestFinished:)];
[_networkQueue setRequestDidFailSelector:@selector(requestFailed:)];
[_networkQueue setQueueDidFinishSelector:@selector(queueFinished:)];
for (int i=1; i<<span style="color: #272ad8">3; i++) {
NSString *strURL = [[NSString alloc] initWithFormat:@"http://www.crazyit.com/service/download.php?email=%@&FileName=test%i.jpg",@"[email protected]",i];
NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.tag = i;
[_networkQueue addOperation:request];
}
[_networkQueue go];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
NSData *data = [request responseData];
// NSLog(@"data = %@",data);
NSError *eror;
NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:&eror];
//如果伺服器是返回的圖片過來,那是通過資料io流過來的,這裡的 resDict值一定會為null, 因為 data不是json格式,所以NSJSONSerialization處理後是不能正解成json格式的。
NSLog(@"resDict = %@",resDict);
if (!resDict) {
UIImage *img = [UIImage imageWithData:data];
if (request.tag ==1) {
_imageView1.image = img;
} else {
_imageView2.image = img;
}
} else {
//如果伺服器是返回的圖片過來,那是通過資料io流過來的,這裡的 resDict值一定會為null, 因為 data不是json格式,所以NSJSONSerialization處理後是不能正解成json格式的。
//如果是返回 json格式,一般類似於 {"ResultCode":-1} 格式,如果是圖片io流返回, 一定不是json格式返回了
NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"];
NSString *errorStr = [resultCodeObj errorMessage];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"錯誤資訊"
message:errorStr
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alertView show];
}
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"請求成功");
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@",[error localizedDescription]);
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"請求失敗");
}
- (void)queueFinished:(ASIHTTPRequest *)request
{
if ([_networkQueue requestsCount] == 0) {
[self setNetworkQueue:nil];
}
NSLog(@"隊列完成");
}
@end
-------------------------
#import
@interface NSNumber (Message)
-(NSString *)errorMessage;
@end
------------------------
#import "NSNumber+Message.h"
@implementation NSNumber (Message)
-(NSString *)errorMessage
{
NSString *errorStr = @"";
switch ([self integerValue]) {
case -9:
errorStr = @"檔案沒有指定。";
break;
case -8:
errorStr = @"檔案找不到。";
break;
case -7:
errorStr = @"沒有資料。";
break;
case -6:
errorStr = @"日期沒有輸入。";
break;
case -5:
errorStr = @"內容沒有輸入。";
break;
case -4:
errorStr = @"ID沒有輸入。";
break;
case -3:
errorStr = @"據訪問失敗。";
break;
case -2:
errorStr = @"您的帳號最多能插入10條資料。";
break;
case -1:
errorStr = @"使用者不存在,請到http://www.crazyit.com註冊。";
default:
break;
}
return errorStr;
}
@end
ASIHTTPRequest架構使用總結系列之阿堂教程4(下載資料)