同步请求:数据会造成主线程阻塞,通常在大数据或者网络不畅的情况下不使用,会使用户与UI失去交互,出现程序的卡死,如果数据量少可以使用同步请求。
异步请求:异步请求不会阻塞主线程,会建立一个新的线程来做操作。异步加载一种方式使用的是block,就算将加载的代码放到主线程去执行,也不会阻塞主线程。异步加载的另一种方式比较灵活。它可以在你需要的时候去启动,在你不需要的时候可以取消。
不管是异步请求还是同步请求,建立连接的步骤上虽然有所差别,但是不体上是一致的:
1、创建NSURL
2、创建Request对象
3、创建NSURLConnection连接

//同步连接的方法
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
//异步连接的方法
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;

异步连接

我们创建三个按钮分别实现 get,post,block方式的异步连接

    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    [button setTitle:@"GET 异步" forState:UIControlStateNormal];
    button.frame = CGRectMake(100, 100, 150, 50);
    [button addTarget:self action:@selector(getAsyn) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button1 setTitle:@"POST 异步" forState:UIControlStateNormal];
    button1.frame = CGRectMake(100, 200, 150, 50);
    [button1 addTarget:self action:@selector(postAsyn) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button1];
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    [button2 setTitle:@"TEST" forState:UIControlStateNormal];
    button2.frame = CGRectMake(100, 300, 150, 50);
    [button2 addTarget:self action:@selector(TEST) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:button2];

get方法中执行代理可以看到下载进度 接受协议:NSURLConnectionDataDelegate

//.h文件
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) AVAudioPlayer *player;//播放mp3
@property (strong,nonatomic) NSMutableData *mData; //post方法用的
@property (nonatomic) long long totalLenght;//下载内容的总长度

NSURLConnectionDataDelegate代理中的方法,可以告诉我们下载情况的
1:接收响应者
2:开始接收数据,此处可以获得下载长度(进度)
3:接收完毕
4:错误的原因方法

//接收响应者
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"%@",response);
    
    //获取数据的总长度
    _totalLenght = [response expectedContentLength];
    NSLog(@"%lld",_totalLenght);
    _mData = [NSMutableData data];
    
    NSLog(@"正在开启加载模式 请稍后…");
}

//开始接收数据
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //当前数据的长度
//    NSLog(@"--------%ld",[data length]);
    [_mData appendData:data];
    NSLog(@"▉▉▉▉▉%.2f%%",100.0*[_mData length] / _totalLenght);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //接收完毕所有的数据之后才会执行的方法
    NSLog(@"播放");
    _player = [[AVAudioPlayer alloc] initWithData:_mData error:nil];
    [_player play];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"%@",error);
}

三个按钮点击事件执行的方法

//get方法 代理
- (void)getAsyn
{
    NSString *str = @"http://yinyueshiting.baidu.com/data2/music/134368947/72104070108000128.mp3?xcode=a7d5ad2a3b062e369dfe3aa462999dd4f8a8b2fa4f50b032";
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    //1.去掉警告的
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
    //2.下面一句
//    [NSURLConnection connectionWithRequest:request delegate:nil];
    
//    NSURLResponse *response = nil; //因为是**类型 下同 所以下面写 &response
//    NSError *error = nil;
//    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];  //同步连接
//    _player = [[AVAudioPlayer alloc] initWithData:data error:nil];
//    [_player play];
}
//post方法 由于上面的百度音乐网址有误 改成了这种才可以
- (void)postAsyn
{
    //post是将URL 和 参数分离的一种请求方式
    NSString *str = @"http://music.baidu.com/data/music/file";
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[@"link=http://yinyueshiting.baidu.com/data2/music/134368947/72104070108000128.mp3?xcode=a7d5ad2a3b062e369dfe3aa462999dd4f8a8b2fa4f50b032" dataUsingEncoding:NSUTF8StringEncoding]];
    
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    [connection start];
//    NSURLResponse *response = nil;
//    NSError *error = nil;
//    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        _player = [[AVAudioPlayer alloc] initWithData:data error:nil];
//        
//        [_player play];
//    }];
    
}
//block的方式
- (void)TEST
{
    NSString *str = @"http://yinyueshiting.baidu.com/data2/music/134368947/72104070108000128.mp3?xcode=a7d5ad2a3b062e369dfe3aa462999dd4f8a8b2fa4f50b032";
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        _player = [[AVAudioPlayer alloc] initWithData:data error:nil];
        
        [_player play];
    }];
}
#import "AppDelegate.h"  
  
@implementation AppDelegate  
  
- (void)dealloc  
{  
    [super dealloc];  
}  
  
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification  
{  
    // Insert code here to initialize your application  
    [self fetchYahooData];  
      
    NSLog(@"-------------------------------------");  
    [self fetchYahooData2_GCD];  
     NSLog(@"-------------------------------------");  
    [self fetchYahooData3];  
}  
//使用异步加载,那么就不会阻塞主线程,因为异步他会开启一个子线程去加载  
  
  
//不使用GCD,同步加在数据,会阻塞主线程  
-(void)fetchYahooData{  
    NSLog(@"同步请求测试开始...");  
    NSString *urlString = @"http://www.yahoo.com";  
    NSURL *url = [NSURL URLWithString:urlString];  
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];  
    NSURLResponse *response = nil;  
    NSError *error = nil;  
    NSLog(@"马上进行同步连接请求url的数据");  
    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];  
    if ([data length] > 0 && error == nil) {  
        NSLog(@"%lu 字节的数据被返回.",(unsigned long)[data length]);  
    }else if([data length] == 0 && error == nil){  
        NSLog(@"没有数据返回");  
    }else if (error != nil){  
        NSLog(@"出现错误= %@",error);  
    }  
    NSLog(@"同步方法测试完成."); //它会等待上面的下载数据完成才打印这句话  
      
}  
  
//使用GCD,同步加载数据.不会阻塞主线程。  
-(void)fetchYahooData2_GCD{  
    NSLog(@"使用GCD同步请求测试开始...");  
    NSString *urlString = @"http://www.yahoo.com";  
    NSLog(@"马上进行同步连接请求url的数据...");  
    dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  
    dispatch_async(dispatchQueue, ^{  
        NSURL *url = [NSURL URLWithString:urlString];  
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];  
        NSURLResponse *response = nil;  
        NSError *error = nil;  
        NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error];  
        if ([data length] > 0 && error == nil) {  
            NSLog(@"%lu 字节的数据被返回.",(unsigned long)[data length]);  
//            [data writeToFile:@"/Users/macoslion/Desktop/new/download.html" atomically:YES]; //下载下来的数据保存为xml,或者html。最好是xml  
              
        }else if ([data length] == 0 && error == nil){  
            NSLog(@"没有数据返回.");  
        }else if (error != nil){  
            NSLog(@"请求出错 = %@",error);  
        }  
    });  
    NSLog(@"GCD测试完成."); //它会直接打印,不会等到上面下载数据完成才打印  
      
}  
  
  
//使用异步,它也不会阻塞主线程  
-(void)fetchYahooData3  
{  
    NSLog(@"异步请求测试开始..");  
    NSString *urlString = @"http://www.yahoo.com";  
    NSURL *url = [NSURL URLWithString:urlString];  
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];  
  
    NSLog(@"马上进行异步连接请求url的数据...");  
      
       
     [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *reponse, NSData *data, NSError *error) {  
         if ([data length] > 0 && error == nil) {  
             NSLog(@"%lu 字节的数据被返回.",(unsigned long)[data length]);  
         }else if([data length] == 0 && error == nil){  
             NSLog(@"没有数据返回");  
         }else if (error != nil){  
             NSLog(@"请求出错 = %@",error);  
         }  
     }];  
     
    NSLog(@"异步方法测试完成"); //这句话不会等到上面打印完了而打印  
  
  
}  
@end  

更多课看->NSURLConnection同步和异步连接 及GCD :http://blog.csdn.net/u012186949/article/details/38047109

 posted on 2015-05-09 17:15  ianhao_cn  阅读(327)  评论(0编辑  收藏  举报