zoukankan      html  css  js  c++  java
  • iOS_GET_网络请求

    同步的 get 请求

    #pragma mark - 同步的 get 请求
    - (IBAction)GETSynButtonDidClicked:(UIButton *)sender {
    
        // 1、网址里面必须写 http://
        NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
    
        // 2、假设网址有汉字须要转换(没有汉字也能够写)
        urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
        // 3、依据字符串创建 url(统一资源定位符)
        NSURL *url = [NSURL URLWithString:urlString];
    
        // 4、依据 url 创建 request 请求类的对象
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
        // 5、開始去请求网络、数据(同步) 返回data
        NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
        // 6、系统自带json解析
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:receiveData options:(NSJSONReadingMutableContainers) error:nil];
    
        NSArray *array = dict[@"news"];
    
        self.newsArray = [NSMutableArray array];
    
        for (NSDictionary *smallDict in array) {
            NewsModal *modal = [[NewsModal alloc] init];
            [modal setValuesForKeysWithDictionary:smallDict];
            [self.newsArray addObject:modal];
        }
    
        for (NewsModal *modal in self.newsArray) {
            NSLog(@"%@", modal.title);
        }
    }

    异步的 get 请求

    #pragma mark - 异步的 get 请求
    - (IBAction)GETAsyButtonDidClicked:(UIButton *)sender {
    
        // 1、拼接 urlString,网址里面必须写 http://
        NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?

    date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"; // 2、依据字符串创建 URL(统一资源定位符) NSURL *url = [NSURL URLWithString:urlString]; // 3、依据 url 创建 request 请求类的对象 NSURLRequest *request = [NSURLRequest requestWithURL:url]; // 4、開始去请求网络、数据(同步) 返回data [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { // 5、系统自带json解析 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil]; NSArray *array = dict[@"news"]; self.newsArray = [NSMutableArray array]; for (NSDictionary *smallDict in array) { NewsModal *modal = [[NewsModal alloc] init]; [modal setValuesForKeysWithDictionary:smallDict]; [self.newsArray addObject:modal]; } for (NewsModal *modal in self.newsArray) { NSLog(@"%@", modal.title); } }]; }

  • 相关阅读:
    css盒子模型
    怎么查看浏览器内核以及浏览器版本
    matlab 读取文件(mat)存储为json文件
    js的闭包
    听别人报告
    关于windows下 python3安装 cython的说明
    python某个module使用了相对引用,同时其__name__又是__main__导致的错误
    python编程指南
    javacc在stanfordnlp中的应用
    hystrix熔断机制修改配置
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/5304590.html
Copyright © 2011-2022 走看看