iOS 网络编程4-发布异步请求

NSURLConnection 常见的发送请求的方法:

同步请求(一般不用):

<span style="font-size:18px;"></span>

<span style="font-size:18px;"> +(NSData *)sendSynchronousRequest:<#(NSURLRequest *)#> returningResponse:<#(NSURLResponse *__autoreleasing *)#> error:<#(NSError *__autoreleasing *)#>;</span>


异步请求:

block回调

<span style="font-size:18px;">+(void)sendAsynchronousRequest:<#(NSURLRequest *)#> queue:<#(NSOperationQueue *)#> completionHandler:<#^(NSURLResponse *response, NSData *data, NSError *connectionError)handler#>
</span>
所以在http://write.blog.csdn.net/postedit/44998611中要修改的就是其中同步请求的方法

<span style="font-size:18px;">    //      //发送一个同步请求(在主线程发送请求),不过一般是不用同步请求的,比较卡住
//    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
   
    
    //发送一个异步请求
    //Queue:存放completionHandler这个任务   设置为主线程
    NSOperationQueue *queue = [NSOperationQueue mainQueue];
    
   [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       //这个block会在请求完毕的时候自动调用
       if(connectionError || data ==nil){
           [self showError:@"请求失败"];
           return;
       }
</span>








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值