開源類庫之一 (ASIHTTPRequest)

來源:互聯網
上載者:User

標籤:iphone開發   xcode   ios   ios開發   apple   

ASIHTTPRequest雖然很久沒有更新了,但是他仍然是一個非常流行的iOS平台網路通訊類庫,使用ASIHTTPRequest之後,大大簡化了iOS平台的網路編程。其以方便的介面對同步、非同步網路傳輸進行了傳輸,將ASIHTTPRequest添加到自己的項目也非常方便,將類庫中所有檔案拷貝到一個檔案夾中,然後將此檔案夾添加到項目中,同時要添加如CFNetWork之下所示的類庫,就可以使用ASIHTTPRequest了:



使用ASIHTTPRequest步驟非常簡答,在一般應用開發中,網路連接基本上使用的都是非同步方式,下面簡單示範一下最簡單的非同步通訊方法


[cpp] view plaincopy
  1. #import "ASIHTTPRequest.h"  
  2.   
  3. - (void) requestDataFromServer  
  4. {  
  5.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
  6.     NSURL* url = [NSURL URLWithString: @"www.fakeurl.com"];  
  7.     ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL: url];  
  8.     [request setTag: 1024];  
  9.     [request setTimeOutSeconds: 3];  
  10.     [request setAllowCompressedResponse:YES];  
  11.     [request setDelegate:self];  
  12.     [request startAsynchronous];  
  13.     [pool drain];  
  14. }  
  15.   
  16. - (void)requestFinished:(ASIHTTPRequest *)request  
  17. {  
  18.     NSString* rawString = [request responseString];  
  19.     if (request.tag == 1024) {  
  20.         //處理網路返回結果  
  21.      }   
  22. }  
  23.   
  24. - (void)requestFailed:(ASIHTTPRequest *)request  
  25. {  
  26.     if (request.tag == 1024) {  
  27.         //處理網路錯誤  
  28.      }   
  29. }   

注意上面的兩個函數中,後面連個為ASIHTTPRequest的delegate函數,其宣告類型不能改變,只要在產生ASIHTTPRequest時的deleage設成了self,那麼最後返回結果,不管是成功調用還是網路失敗,都會調用這兩個函數中的對應的一個。

開源類庫之一 (ASIHTTPRequest)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.