background image

iOS 网络编程异步 GET 方法请求编程

    异步请求会使用 NSURLConnection 委托协议 NSURLConnectionDelegate。在请求不同阶段
会回调委托对象方法。

NSURLConnectionDelegate 协议的方法有:

    connection:didReceiveData: 请求成功,开始接收数据,如果数据量很多,它会被多次调
用;

    connection:didFailWithError: 加载数据出现异常;

    connectionDidFinishLoading: 成功完成加载数据,在 connection:didReceiveData 方法之后
执行;

    使用异步请求的主视图控制器 MasterViewController.h 代码如下:

#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;