IOS NSURLConnection 同步下载获取数据

{app.params.name}} {app.params.name}} {app.params.name}} 同步下载(交互不好,容易出现卡死现象,一般下载数据较小或有特定需求才使用)。  发送同步请求后,程序将停止用户交互,直到服务器返回数据完成后,才进行下一步的操作。

步骤:

1.创建NSURL

NSURL *url = [[NSURL alloc] initWithString:@"http://www.baidu.com/"];

2.通过URL创建NSURLRequest

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15];

cachePolicy 缓存协议是个枚举类型:

NSURLRequestUseProtocolCachePolicy 基础策略

NSURLRequestReloadIgnoringLocalCacheData 忽略本地缓存

NSURLRequestReturnCacheDataElseLoad 首先使用缓存,如果没有本地缓存,才从原地址下载

NSURLRequestReturnCacheDataDontLoad 使用本地缓存,从不下载,如果本地没有缓存,则请求失败。此策略多用于离线操作

NSURLRequestReloadIgnoringLocalAndRemoteCacheData 无视任何的缓存策略,无论是本地还是远程,总是从原地址重新下载

NSURLRequestReloadRevalidatingCacheData 如果本地缓存是有效的则不下载。其他任何情况都从原地址重新下载

3.建立网络连接NSURLConnection,同步请求数据

NSData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRwquest:request returningResponse:&response error:&error];

以上三步后,就需要将receivedData进行解析,一般是XML/JSON

实例演示:

detailViewController.h

#import <UIKit/UIKit.h>

@interface vlinkagePersonViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>

//生成content的key值
@property (strong, nonatomic) NSArray *keys;
//生成content的value值
@property (strong, nonatomic) NSArray *objects;
//列表内容
@property (strong, nonatomic) NSDictionary *content;

//艺人的数据
@property (retain, nonatomic) NSMutableData *personData;

//艺人的数组数据
@property (retain, nonatomic) NSArray *person;
@end
detailViewController.m

#import "vlinkagePersonViewController.h"

@interface vlinkagePersonViewController ()

@end

@implementation vlinkagePersonViewController

@synthesize content;
@synthesize personData;
@synthesize person;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    
    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:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60];
    
    NSURLResponse *response = [[NSURLResponse alloc] init];
    NSError *error = [[NSError alloc] init];
    
    NSMutableData *receivedData = (NSMutableData *)[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    
    self.person = [self readJsonData:receivedData];
    
    if([self.person isKindOfClass:[NSDictionary class]]){
        NSLog(@"NSDictionary");
    }
    
    if([self.person isKindOfClass:[NSArray class]]){
        NSLog(@"NSArray");
    }

}

-(NSArray *) readJsonData:(NSMutableData *)data{
    //NSJSONSerialization提供了将JSON数据转换为Foundation对象(一般都是NSDictionary和NSArray)
    //和Foundation对象转换为JSON数据(可以通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据)。
    NSError *error;
    NSDictionary *personDictionary = [NSJSONSerialization JSONObjectWithData:data
                                                               options:NSJSONReadingMutableContainers
                                                                 error:&error];
    NSDictionary *personInfo = [personDictionary objectForKey:@"data"];
    
    NSArray *personList = [personInfo objectForKey:@"list"];
    
    return personList;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.person count];
}


-(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];
        
    }

    NSDictionary *dic=[self.person objectAtIndex:indexPath.row];
    NSString *name=[dic objectForKey:@"zh_name"];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = name;
    
    return cell;
    
}
@end
结果还会讲数据显示到table中


版权声明

davidzhang 创作并维护的 Gowhich 博客采用 创作共用保留署名-非商业-禁止演绎4.0国际许可证。

本文首发于 Gowhich 博客( https://www.gowhich.com ),版权所有,侵权必究。

本文永久链接: https://www.gowhich.com/blog/60

comments powered by Disqus