iOS網路編程-ASIHTTPRequest小例子-資料請求隊列

來源:互聯網
上載者:User

執行個體:請求隊列

我們通過一個例子介紹一下請求隊列使用,我們設計了一個應用,使用者點擊GO按鈕從伺服器同時下載兩張圖片顯示在畫面中。

 

我們直接看看主視圖控制器ViewController.h代碼如下:

#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

 

 

我 們需要引入ASI架構的兩個標頭檔ASIHTTPRequest.h和ASINetworkQueue.h。其中imageView1和 imageView2是與畫面對應的兩個圖片視圖控制項。還定義了ASINetworkQueue  類型的networkQueue屬性。我們直接看看主視圖控制器ViewController.m中點擊GO按鈕調用方法,代碼如下:

- (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<3; i++) {NSString *strURL = [[NSString alloc] initWithFormat:@”http://iosbook3/download.php?email=%@&FileName=test%i.jpg”,@”<你的iosbook1.com使用者郵箱>”,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];NSError *eror;NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingAllowFragments error:&eror];if (!resDict) {UIImage *img = [UIImage imageWithData:data];if (request.tag ==1) {                                                ①_imageView1.image = img;} else {_imageView2.image = img;}} else {NSNumber *resultCodeObj = [resDict objectForKey:@"ResultCode"];NSString *errorStr = [resultCodeObj errorMessage];UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@”錯誤資訊”message:errorStrdelegate:nilcancelButtonTitle:@”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(@”隊列完成”);}

 

 

requestFinished: 方法是請求對象成功回調方法,因此有兩個請求對象它會被調用兩次,在第①行代碼中我們根據GO按鈕點擊事件設定的 請求對象的tag屬性,來判斷是哪個請求對象的回調。進而載入到顯示不同的圖片視圖。第②代碼[_networkQueue requestsCount]可以判斷隊列中請求對象的個數。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.