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

来源:互联网 发布:寻找满月英知 编辑:程序博客网 时间:2024/05/18 01:07

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