iOS開發那些事-iOS網路編程非同步GET方法請求編程

來源:互聯網
上載者:User

connection:didReceiveData: 請求成功,開始接收資料,如果資料量很多,它會被多次調用;

connection:didFailWithError: 載入資料出現異常;

connectionDidFinishLoading: 成功完成載入資料,在connection:didReceiveData方法之後執行;

使用非同步請求的主視圖控制器MasterViewController.h代碼如下:

[cpp]  ?#import <UIKit/UIKit.h>  
 
#import “NSString+URLEncoding.h”  
 
#import “NSNumber+Message.h”  
 
  
 
@interface MasterViewController : UITableViewController <NSURLConnectionDelegate> 
 
  
 
@property (strong, nonatomic) DetailViewController *detailViewController; 
 
//儲存資料列表  
 
@property (nonatomic,strong) NSMutableArray* listData; 
 
  
 
//接收從伺服器返回資料。  
 
@property (strong,nonatomic) NSMutableData *datas; 
 
  
 
//重新載入表視圖  
 
-(void)reloadView:(NSDictionary*)res; 
 
  
 
//開始請求Web Service  
 
-(void)startRequest; 
 
  
 
@end 

#import <UIKit/UIKit.h>

#import “NSString+URLEncoding.h”

#import “NSNumber+Message.h”

 

@interface MasterViewController : UITableViewController <NSURLConnectionDelegate>

 

@property (strong, nonatomic) DetailViewController *detailViewController;

//儲存資料列表

@property (nonatomic,strong) NSMutableArray* listData;

 

//接收從伺服器返回資料。

@property (strong,nonatomic) NSMutableData *datas;

 

//重新載入表視圖

-(void)reloadView:(NSDictionary*)res;

 

//開始請求Web Service

-(void)startRequest;

 

@end


上面的代碼在MasterViewController定義中實現了NSURLConnectionDelegate協議。datas屬性用來存放從伺服器返回的資料,定義為可變類型,是為了從伺服器載入資料過程中不斷地追加到這個datas中。MasterViewController.m代碼如下:

[cpp]  /*
 
* 開始請求Web Service
 
*/ 
 
-(void)startRequest { 
 
NSString *strURL = [[NSString alloc] initWithFormat: 
 
@”http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@”,  
 
@”<你的iosbook1.com使用者郵箱>”,@”JSON”,@”query”]; 
 
NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]]; 
 
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; 
 
NSURLConnection *connection = [[NSURLConnection alloc] 
 
initWithRequest:request 
 
delegate:self]; 
 
if (connection) { 
 
_datas = [NSMutableData new]; 
 

 

 
  
 
#pragma mark- NSURLConnection 回調方法  
 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { ① 
 
[_datas appendData:data]; 
 

 
  
 
-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error { 
 
NSLog(@”%@”,[error localizedDescription]); 
 

 
- (void) connectionDidFinishLoading: (NSURLConnection*) connection {         ② 
 
NSLog(@”請求完成…”); 
 
NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:_datas 
 
options:NSJSONReadingAllowFragments error:nil]; 
 
[self reloadView:dict]; 
 

/*

* 開始請求Web Service

*/

-(void)startRequest {

NSString *strURL = [[NSString alloc] initWithFormat:

@”http://iosbook3/mynotes/webservice.php?email=%@&type=%@&action=%@”,

@”<你的iosbook1.com使用者郵箱>”,@”JSON”,@”query”];

NSURL *url = [NSURL URLWithString:[strURL URLEncodedString]];

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

NSURLConnection *connection = [[NSURLConnection alloc]

initWithRequest:request

delegate:self];

if (connection) {

_datas = [NSMutableData new];

}

}

 

#pragma mark- NSURLConnection 回調方法

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { ①

[_datas appendData:data];

}

 

-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error {

NSLog(@”%@”,[error localizedDescription]);

}

- (void) connectionDidFinishLoading: (NSURLConnection*) connection {         ②

NSLog(@”請求完成…”);

NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:_datas

options:NSJSONReadingAllowFragments error:nil];

[self reloadView:dict];

}


在第①行的connection:didReceiveData:方法中,通過[_datas appendData:data]語句不斷地接收伺服器端返回的資料,理解這一點是非常重要的。如果載入成功就回調第②行的connectionDidFinishLoading:方法,這個方法被回調也意味著這次請求的結束,這時候_datas中的資料是完整的,在這裡把資料發送回展示層的視圖控制器。

 


 

聯繫我們

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