zoukankan      html  css  js  c++  java
  • 异步请求网络代码(一)

    //
    //  ViewController.m
    //  01-网络请求
    //
    //  Created by jerry on 15/9/24.
    //  Copyright (c) 2015年 jerry. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        // 1.url
        NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/demo.json"];
        // 2.建立请求
    //    NSURLRequest *request = [NSURLRequest requestWithURL:url];
        /**
         *  requestWithURL:url,资源路径
         *  cachePolicy:缓存策略 
         
         NSURLRequestUseProtocolCachePolicy = 0,  // 默认缓存策略,会自动缓存。
         
         NSURLRequestReloadIgnoringLocalCacheData = 1, // Reload 刷新  Ignoring 忽略 Local 本地CacheData 缓存数据    整体就是忽略本地缓存数据,每次都需要从服务器获取数据.
         // 两个都是用来做离线访问的。一般配合reachablility(苹果提供的检测网络连接框架)配合使用。
         // 如果用户使用wifi  就使用这个策略(国内一般都使用这个策略不管是不是wifi 还是3g)
         NSURLRequestReturnCacheDataElseLoad = 2, // 如果有缓存,就用缓存,如果没有就上网加载 (hot  用的比较多)
         // 如果用户使用3g 网络,使用这个策略
         NSURLRequestReturnCacheDataDontLoad = 3, // 如果没有缓存,就缓存,如果没有缓存就返回空。 (hot 用的比较多)
         
         
         *  timeoutInterval:超时时间,默认是60s   一般设置15~20,超过这个时间以后,服务器还没有响应就不继续等待 。
         *  SDWebImage 超时时长,设置的事15s 。
         *  AFN 超时时长 60s。
         */
        NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:15];
        
        // 3. 发送请求
        /**
         sendAsynchronousRequest:request 参数
         queue:队列,这个队列完成后,回调block执行队列。
           Asynchronous:只要是异步,肯定会开新的线程。
         
         // 使用场景:如果下载的事压缩包。解压缩也是一个耗时操作。
         如果回调block里面只需要更新ui 那么就可以在开始就制定queue为主队列。
         handler:网络访问完成以后执行的代码块。
         response:服务器的响应(包含响应行/响应头。。。一般不关心这个东西,只有在下载的时候才会关心这个操作)
         data:服务器返回的二进制数据
         connectionError:服务器返回的错误信息,只要是有网络访问,就会有可能有错误。
         */
        [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (connectionError || data == nil) { // 如果为真,说明有错误
                NSLog(@"网络不给力,请稍后再试。。。");
                NSLog(@"%@",connectionError);
                return ;
            }
            NSLog(@"解压缩。。%@",[NSThread currentThread]);
            dispatch_async(dispatch_get_main_queue(), ^{
                NSLog(@"更新ui ");
                 NSLog(@"%@", [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
            });
           
        }];
        
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

    报错。。。。

    2015-09-24 15:50:12.779 01-网络请求[839:20612] 解压缩。。<NSThread: 0x7fa171c47cd0>{number = 3, name = (null)}
    2015-09-24 15:50:12.836 01-网络请求[839:20542] 更新ui 
    2015-09-24 15:50:12.836 01-网络请求[839:20542] <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>404 Not Found</title>
    </head><body>
    <h1>Not Found</h1>
    <p>The requested URL /demo.json was not found on this server.</p>
    </body></html>
  • 相关阅读:
    LeetCode 453 Minimum Moves to Equal Array Elements
    LeetCode 112 Path Sum
    LeetCode 437 Path Sum III
    LeetCode 263 Ugly Number
    Solutions and Summay for Linked List Naive and Easy Questions
    AWS–Sysops notes
    Linked List
    All About Linked List
    datatable fix error–Invalid JSON response
    [转]反编译c#的相关问题
  • 原文地址:https://www.cnblogs.com/pengpengzhang/p/4835583.html
Copyright © 2011-2022 走看看