NSURLConnection 下載數據 -- IOS(實例)

iPhone網絡開發中如何使用NSURLConnection是本文要介紹的內容,這篇文章是翻譯的蘋果官方文檔,想要看英文原版的可以到蘋果網站查看,來看詳細內容。

 

NSURLConnection 提供了很多靈活的方法下載URL內容也提供了一個簡單的接口去創建和放棄連接,同時使用很多的delegate方法去支持連接過程的反饋和控制

 

如何創建一個連接呢?

 

爲了下載url的內容,程序需要提供一個delegate對象,並且至少實現下面的方法

C代碼  收藏代碼
  1. - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSHTTPURLResponse*)response  
  2. - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data  
  3. - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error  
  4. - (void)connectionDidFinishLoading:(NSURLConnection *)connection  

 

NSURLConnect還提供了一個方便的類方法(class method) : sendSynchronousRequest:returningResponse:error: 可用來 同步地加載一個URL請求

C代碼  收藏代碼
  1. + (NSData *)sendSynchronousRequest:    (NSURLRequest *)request      returningResponse:   (NSURLResponse **)response    error:  (NSError **)error  
 

1. request 要裝載的URL請求. 這個request 對象 作爲初始化進程的一部分,被深度複製(deep-copied). 在這個方法返回之後, 再修改request, 將不會影響用在裝載的過程中的request

2. reponse 輸出參數, 由服務器返回的URL響應

3. error   輸出參數, 如果在處理請求的過程中發生錯誤,就會使用.  無錯誤,就爲NULL

 

舉例一

 

1、先創建一個NSURL

2、在通過NSURL創建NSURLRequest,可以指定緩存規則和超時時間

3、創建NSURLConnection實例,指定NSURLRequest和一個delegate對象

 

如果創建失敗,則會返回nil,如果創建成功則創建一個NSMutalbeData的實例用來存儲數據

 

代碼:

C代碼  收藏代碼
  1. NSURLRequest *theRequest=[NSURLRequest requestWithURL:    
  2.                   [NSURL URLWithString:@“http://www.sina.com.cn/”]    
  3.                  cachePolicy:NSURLRequestUseProtocolCachePolicy    
  4.                  timeoutInterval:60.0];    
  5. NSURLConnection *theConncetion=[[NSURLConnection alloc]         
  6.                    initWithRequest:theRequest delegate:self];    
  7. if(theConnection)    
  8. {    
  9. //創建NSMutableData    
  10.   receivedData=[[NSMutableData data] retain];    
  11. }else // 創建失敗   

 

C代碼  收藏代碼
  1. NSURLRequestUseProtocolCachePolicy //NSURLRequest默認的cache policy, 是最能保持一致性的協議。  
  2. NSURLRequestReloadIgnoringCacheData  //忽略緩存直接從原始地址下載  
  3. NSURLRequestReturnCacheDataElseLoad  //只有在cache中不存在data時才從原始地址下載  
  4. NSURLRequestReturnCacheDataDontLoad  //允許app確定是否要返回cache數據,如果使用這種協議當本地不存在response的時候,創建NSURLConnection or NSURLDownload實例時將會馬上返回nil;這類似於離線模式,沒有建立網絡連接;  
 

NSURLConnection還有幾個初始化函數,有個初始化函數可以做到創建連接但是並不馬上開始下載,而是通過start:開始

 

當收到initWithRequest: delegate: 消息時,下載會立即開始,在代理(delegate)收到connectionDidFinishLoading:或者 connection:didFailWithError:消息之前可以通過給連接發送一個cancel:消息來中斷下載。

 

當服務器提供了足夠客戶程序創建NSURLResponse對象的信息時,代理對象會收到一個connection:didReceiveResponse:消息,在消息內可以檢查NSURLResponse對象和確定數據的預期長途,mime類型,文件名以及其他服務器提供的元信息

 

要注意,一個簡單的連接也可能會收到多個connection:didReceiveResponse:消息當服務器連接重置或者一些罕見的原因(比如多組mime文檔),代理都會收到該消息這時候應該重置進度指示,丟棄之前接收的數據

C代碼  收藏代碼
  1. -(void)connection:(NSURLConnection *) connectiondidReceiveResponse:    
  2.                         (NSURLResponse*)response    
  3. {   
  4.    [receiveData setLength:0];    
  5. }  
 

當下載開始的時候,每當有數據接收,代理會定期收到connection:didReceiveData:消息代理應當在實現中儲存新接收的數據,下面的例子既是如此

C代碼  收藏代碼
  1. -(void) connection:(NSURLConnection *) connection didReceiveData:    
  2.             (NSData *) data    
  3. {    
  4.    [receiveData appendData:data];    
  5.   
  6. }   
 

在上面的方法實現中,可以加入一個進度指示器,提示用戶下載進度

 

當下載的過程中有錯誤發生的時候,代理會收到一個connection:didFailWithError消息,消息參數裏面的NSError對象提供了具體的錯誤細節,它也能提供在用戶信息字典裏面失敗的url請求(使用NSErrorFailingURLStringKey)

 

當代理接收到連接的connection:didFailWithError消息後,對於該連接不會在收到任何消息

 

舉例

C代碼  收藏代碼
  1. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error    
  2. {   
  3. [connection release];    
  4.   
  5.   [receivedData release];    
  6.    NSLog(@"Connection failed! Error - %@ %@",    
  7.           [error localizedDescription],    
  8.           [[error userInfo] objectForKey:NSErrorFailingURLStringErrorKey]);    
  9. }  
 

最後,如果連接請求成功的下載,代理會接收connectionDidFinishLoading:消息代理不會收到其他的消息了,在消息的實現中,應該釋放掉連接

 

舉例:

C代碼  收藏代碼
  1. -(void)connectionDidFinishLoading:(NSURLConnection *)connection    
  2. {  
  3.    //do something with the data    
  4.   NSLog(@"succeeded  %d byte received",[receivedData length]);   
  5.   
  6. [connection release];    
  7. [receivedData release];    
  8. }  
 

一個實現異步get請求的例子:

C代碼  收藏代碼
  1. NSString *url = [NSString stringWithFormat:@"http://localhost/chat/messages.php?past=%ld&t=%ld",  
  2.                      lastId, time(0) ];  
  3.       
  4.     NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];  
  5.     [request setURL:[NSURL URLWithString:url]];  
  6.     [request setHTTPMethod:@"GET"];  
  7.   
  8.     NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];    
  9.     if (conn)  
  10.     {    
  11.         receivedData = [[NSMutableData data] retain];    
  12.     }     
  13.     else     
  14.     {    
  15.     }   
  16.   
  17. - (void)timerCallback {  
  18.     //[timer release];  
  19.     [self getNewMessages];  
  20. }  
  21.   
  22. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response    
  23. {    
  24.     [receivedData setLength:0];    
  25. }    
  26.   
  27. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data    
  28. {    
  29.     [receivedData appendData:data];    
  30. }    
  31.   
  32. - (void)connectionDidFinishLoading:(NSURLConnection *)connection    
  33. {    
  34.     if (chatParser)  
  35.         [chatParser release];  
  36.       
  37.     if ( messages == nil )  
  38.         messages = [[NSMutableArray alloc] init];  
  39.   
  40.     chatParser = [[NSXMLParser alloc] initWithData:receivedData];  
  41.     [chatParser setDelegate:self];//set the delegate  
  42.     [chatParser parse];//start parse  
  43.   
  44.     [receivedData release];    
  45.       
  46.     [messageList reloadData];  
  47.       
  48.     NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:  
  49.                                     [self methodSignatureForSelector: @selector(timerCallback)]];  
  50.     [invocation setTarget:self];  
  51.     [invocation setSelector:@selector(timerCallback)];  
  52.     //timer = [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];  
  53.     [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:invocation repeats:NO];//if set yes,then very 5 seconds updata the table  
  54. }   
 

一個實現同步Get請求的例子:

C代碼  收藏代碼
  1. // 初始化請求  
  2. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];           
  3. // 設置URL  
  4. [request setURL:[NSURL URLWithString:urlStr]];  
  5. // 設置HTTP方法  
  6. [request setHTTPMethod:@"GET"];  
  7. // 發 送同步請求, 這裏得returnData就是返回得數據了  
  8. NSData *returnData = [NSURLConnection sendSynchronousRequest:request   
  9.                                                returningResponse:nil error:nil];   
  10. // 釋放對象  
  11. [request release];  
 

 

來源:

http://mobile.51cto.com/iphone-281460.htm

http://blog.csdn.net/bl1988530/article/details/6590099

發佈了45 篇原創文章 · 獲贊 6 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章