zoukankan      html  css  js  c++  java
  • IOS之网络数据下载和JSON解析

     IOS之网络数据下载和JSON解析

      简介

        在本文中笔者要给大家介绍ios中如何利用NSURLConnnection从网络上下载数据,以及如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载显示。

      涉及到的知识点:

      1.NSURLConnection异步下载和封装
      2.JSON格式和JSON格式解析

      3.数据显示和使用SDWebImage异步显示图片

    内容

      1.网络下载基础知识介绍

          什么是网络应用?

           网络应用的程序结构

           常见的网络接口形式

           常见的数据格式

          界面开发的一般流程

      2. NSURLConnection使用

    #import "ZJHttpRequest.h"
    
    //消除performSelector的警告
    #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    
    //类扩展
    //项目实践:
    //  有些实例变量内部使用, 不想放在头文件中, 放在这儿
    @interface ZJHttpRequest ()<NSURLConnectionDataDelegate>
    {
        NSURLConnection *_connection;
        NSString *_url;
        id _target;
        SEL _action;
    }
    @end
    
    @implementation ZJHttpRequest
    //作用:
    //  传入网址, 下载完成执行后执行target对象中action方法
    -(void)requestWithUrl:(NSString *)url
                   target:(id)target
                   action:(SEL)action
    {
        _url = url;
        _target = target;
        _action = action;
        
        //发起URL请求
        _data = [[NSMutableData alloc] init];
        _connection = [[NSURLConnection alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] delegate:self startImmediately:YES];
    }
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [_data appendData:data];
    }
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        //下载完成了, 执行保存的方法
        if(_target && [_target respondsToSelector:_action])
        {
            [_target performSelector:_action withObject:self];
        }
        
    }
    @end

      3. JSON格式说明和格式化工具

    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        //数据接口
        NSString *urlString = @"http://iappfree.candou.com:8080/free/applications/limited?currency=rmb&page=1&category_id=";
        
        _dataArray = [[NSMutableArray alloc] init];
        
        //下载
        _request = [[ZJHttpRequest alloc] init];
        [_request requestWithUrl:urlString target:self action:@selector(dealDowloadFinish:)];
        
        [self createTableView];
    }
    #pragma mark - createTableView
    -(void)createTableView
    {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        [self.view addSubview:_tableView];
    }
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _dataArray.count;
    }
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellID = @"cell";
        AppCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if(cell == nil)
        {
            cell = [[AppCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        }
        //config cell
        AppModel *model = _dataArray[indexPath.row];
        //cell.textLabel.text = model.name;
        cell.nameLabel.text = model.name;
        
        [cell.iconImageView setImageWithURL:[NSURL URLWithString:model.iconUrl]];
        
        
        return cell;
    }
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 80;
    }
    
    -(void)dealDowloadFinish:(ZJHttpRequest *)request
    {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:request.data options:NSJSONReadingMutableContainers error:nil];
        //NSLog(@"dict = %@",dict);
        NSArray *appList = dict[@"applications"];
        for (NSDictionary *appDict in appList) {
            AppModel *model = [[AppModel alloc] init];
            model.applicationId = appDict[@"applicationId"];
            model.name = appDict[@"name"];
            model.iconUrl = appDict[@"iconUrl"];
            [_dataArray addObject:model];
        }
        
        //下载数据刷新显示
        [_tableView reloadData];
    }

      4.一个完成页面点实现(包括model的创建,SDWebImage 的使用)

     效果图

  • 相关阅读:
    [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)
    Error: 17053 LogWriter: Operating system error 21(The device is not ready.)
    PPS2013校园招聘笔试题
    RobotFrameWork(十一)AutoItLibrary测试库在win7(64bit)下安装及简单使用
    iOS 5 故事板入门(3)
    Eclipse代码字体、颜色美化,更改字体大小、颜色
    iOS 5 故事板入门(4)
    54. 如何在测试中触发服务器上运行的代理
    【PHP SDK for OpenStack/Rackspace APIs】身份验证
    JQuery总结
  • 原文地址:https://www.cnblogs.com/SuperHanks/p/4384526.html
Copyright © 2011-2022 走看看