异步下载支持应用程序在后台下载数据,在等待下载完成的过程中不会阻塞代码的运行
代码如下:
detailViewController.h1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #import <UIKit/UIKit.h>
@interface vlinkagePersonViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) NSArray *keys;
@property (strong, nonatomic) NSArray *objects;
@property (strong, nonatomic) NSDictionary *content;
@property (retain, nonatomic) NSMutableData *personData;
@end
|
detailViewController.m1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| #import "vlinkagePersonViewController.h"
@interface vlinkagePersonViewController ()
@end
@implementation vlinkagePersonViewController
@synthesize content; @synthesize personData;
- (void)viewDidLoad { [super viewDidLoad];
NSURL *url = [[NSURL alloc] initWithString:@"http://xxx.xxx.xxx.xxx:xxx/person/actorlist?order_by=index&start_date=2012-06-01&end_date=2012-07-01&start=1&offset=10&app_key=KSKdzSyeb99YdLwTMrzvuLumNYCM6pzT4Z3f27R4L3qq6jCs"]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if(!connection){ NSLog(@"链接失败!"); }else{ self.personData = [NSMutableData data]; self.content = [[NSDictionary alloc] init]; } NSLog(@"content = %@",self.content); }
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSLog(@"didReceiveResponse"); [self.personData setLength:0]; }
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ NSLog(@"didReceiveData"); [self.personData appendData:data]; }
- (void)connectionDidFinishLoading: (NSURLConnection *) connection{ NSLog(@"connectionDidFinishLoading"); [self readJsonData]; NSLog(@"content %@",self.content); }
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:[error localizedDescription] message:[error localizedFailureReason] delegate:self cancelButtonTitle:@"OK!" otherButtonTitles:nil]; [errorAlert show]; }
-(void) readJsonData{ NSError *error; NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:self.personData options:NSJSONReadingMutableContainers error:&error]; NSDictionary *personInfo = [personDictionary objectForKey:@"data"]; NSDictionary *personList = [personInfo objectForKey:@"list"]; self.content = personList; NSLog(@"personList %@",personList); }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{ return 1; }
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil){ cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } cell.textLabel.text=@"话题"; return cell; } @end
|
输出的结果是:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| 2013-04-26 16:45:55.174 vlinkagePerson3[67809:c07] content = { } 2013-04-26 16:45:55.501 vlinkagePerson3[67809:c07] didReceiveResponse 2013-04-26 16:45:55.501 vlinkagePerson3[67809:c07] didReceiveData 2013-04-26 16:45:55.502 vlinkagePerson3[67809:c07] connectionDidFinishLoading 2013-04-26 16:45:55.502 vlinkagePerson3[67809:c07] personList ( { id = 175; "is_attention" = 0; score = "7.84948333"; "zh_name" = "\U5b59\U4fea"; }, { id = 1582; "is_attention" = 0; score = "7.78961667"; "zh_name" = "\U949f\U6c49\U826f"; }, { id = 17577; "is_attention" = 0; score = "7.69375000"; "zh_name" = "\U5f20\U6839\U7855"; }, { id = 35; "is_attention" = 0; score = "7.60104167"; "zh_name" = "\U6768\U5e42"; }, { id = 3880; "is_attention" = 0; score = "7.56774167"; "zh_name" = "\U6797\U4f9d\U6668"; }, { id = 94; "is_attention" = 0; score = "7.56668333"; "zh_name" = "\U674e\U5c0f\U7490"; }, { id = 504; "is_attention" = 0; score = "7.48178333"; "zh_name" = "\U5f20\U6aac"; }, { id = 3571; "is_attention" = 0; score = "7.46722500"; "zh_name" = "\U9a6c\U5929\U5b87"; }, { id = 228; "is_attention" = 0; score = "7.45210000"; "zh_name" = "\U5b8b\U4e39\U4e39"; }, { id = 329; "is_attention" = 0; score = "7.44270833"; "zh_name" = "\U80e1\U6b4c"; } ) 2013-04-26 16:45:55.503 vlinkagePerson3[67809:c07] content ( { id = 175; "is_attention" = 0; score = "7.84948333"; "zh_name" = "\U5b59\U4fea"; }, { id = 1582; "is_attention" = 0; score = "7.78961667"; "zh_name" = "\U949f\U6c49\U826f"; }, { id = 17577; "is_attention" = 0; score = "7.69375000"; "zh_name" = "\U5f20\U6839\U7855"; }, { id = 35; "is_attention" = 0; score = "7.60104167"; "zh_name" = "\U6768\U5e42"; }, { id = 3880; "is_attention" = 0; score = "7.56774167"; "zh_name" = "\U6797\U4f9d\U6668"; }, { id = 94; "is_attention" = 0; score = "7.56668333"; "zh_name" = "\U674e\U5c0f\U7490"; }, { id = 504; "is_attention" = 0; score = "7.48178333"; "zh_name" = "\U5f20\U6aac"; }, { id = 3571; "is_attention" = 0; score = "7.46722500"; "zh_name" = "\U9a6c\U5929\U5b87"; }, { id = 228; "is_attention" = 0; score = "7.45210000"; "zh_name" = "\U5b8b\U4e39\U4e39"; }, { id = 329; "is_attention" = 0; score = "7.44270833"; "zh_name" = "\U80e1\U6b4c"; } )
|