iOS development network part -- complex JSON analysis data display

Keywords: Attribute JSON network MediaPlayer

The data in the tableView below is downloaded from the network (server). Normally, clicking each cell will play the video. The url address of the video here is wrong. Therefore, the playback is wrong. Normally, the video can be played!

Format online http://tool.oschina.net/codeformat/json

The renderings are as follows:



This project uses two third-party frameworks: SDWebImage and mjextensen

SDWebImage: this framework is used when downloading pictures from the network to the local. It solves the problems of memory cache, disk cache, repeated download, etc

Mjextensen: used for data conversion, dictionary to model, model to array, etc

//1. Convert dictionary array to model array
    //Using mjextensen framework for dictionary transformation model
        self.videos = [XMGVideo objectArrayWithKeyValuesArray:videoArray];

//2. Rename the name of the model property
//The first method is to rename attribute names, which is somewhat code intrusive
//Set the ID in the dictionary to be replaced by the ID in the model
+(NSDictionary *)replacedKeyFromPropertyName
{
    return @{
             @"ID":@"id"
             };
}

//The second method is to rename attribute names. The code is zero intrusive
    [XMGVideo setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                 @"ID":@"id"
                 };
    }];

//3. Internal implementation principle of mjextension framework - runtime


General steps of the project:

1. Create a uitableviewcontroller. The cell type is set to Subtitle

2. Parse the response body data returned from the server (deserialization), and use an array to receive it (mjextense framework is required for dictionary array to model array)

3. The data in the array is assigned to the model attribute (SDWebImage framework is used when setting data for pictures)

4. When you click cell, the video will be played simply


The code is as follows:

ZYVideo model file

//
//  ZYVideo.h
//  06 master complex JSON parsing data display
//
//  Created by Chaoyang on December 10, 2017
//  Copyright © 2017 sunny. All rights reserved
//

#import <Foundation/Foundation.h>

@interface ZYVideo : NSObject

/* ID */
@property (nonatomic, strong) NSString *ID;
/* Picture address */
@property (nonatomic, strong) NSString *image;
/* Playback time */
@property (nonatomic, strong) NSString *length;
/* Title */
@property (nonatomic, strong) NSString *name;
/* The url of the video */
@property (nonatomic, strong) NSString *url;


@end

//#import "MJExtension.h"

@implementation ZYVideo

// It's too invasive to use this way. Use another way
// id replace id
//+ (NSDictionary *)mj_replacedKeyFromPropertyName
//{
//    return @{
//        
//        @"ID" : @"id"
//        
//    };
//}

@end

ViewController file

//  ZYTableViewController.m
//  06 master complex JSON parsing data display
//
//  Created by Chaoyang on December 7, 2017
//  Copyright © 2017 sunny. All rights reserved
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import <MediaPlayer/MediaPlayer.h>
#import "ZYVideo.h"
#import "MJExtension.h"

#define baseUrlStr @"http://120.25.226.186:32812"

@interface ViewController ()
/* Array of storage models */
@property (nonatomic, strong) NSMutableArray *videos;

@end

@implementation ViewController

- (NSMutableArray *)videos
{
    if (!_videos) {
        _videos = [NSMutableArray array];
    }
    return _videos;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // The name of the attribute in the replacement model conflicts with the system key
    [ZYVideo mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
        return @{
                 @"ID" : @"id"
                 };
    }];
    
    //1. Determine url
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/video?method=get&type=JSON"];
    //2. Create request
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //3. Create asynchronous connection
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        
        // fault tolerant 
        if (connectionError) {
            return ;
        }
        
        // 4. Parse data (deserialization)
        NSDictionary *dictM = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        
        // Using the third-party framework: dictionary array to model array
        self.videos = [ZYVideo mj_objectArrayWithKeyValuesArray:dictM[@"videos"]];
        //5. Refresh UI
        [self.tableView reloadData];
        
    }];
}

#Pragma mark tableview data source method
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.videos.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1. Set reuse ID
    static NSString *ID = @"video";
    //2. Reuse the cell in the cache pool (if not, it will be created automatically)
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    //3. Set data
//    NSDictionary *dict = self.videos[indexPath.row];
    ZYVideo *video = self.videos[indexPath.row];
    
    cell.textLabel.text = video.name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"Playback time:%@",video.length];
    // Use SDWebImage to set up pictures downloaded in the network
    // url of mosaic picture
//    NSString *baseUrlStr = @"http://120.25.226.186:32812";
    NSString *urlStr = [baseUrlStr stringByAppendingPathComponent:video.image];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"qq"]];
    
    NSLog(@"----%@",video.ID);
    
    return cell;
    
}

#Proxy method of pragma mark tableview
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //1. Get the data
//    NSDictionary *dict = self.videos[indexPath.row];
    ZYVideo *video = self.videos[indexPath.row];
    //2. Splicing resource path
    NSString *urlStr = [baseUrlStr stringByAppendingPathComponent:video.url];
    //3. Create player
    MPMoviePlayerViewController *mpc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:urlStr]];
    //4. Eject controller
    [self presentViewController:mpc animated:YES completion:nil];
}

@end



Posted by mayus on Sat, 09 May 2020 09:58:04 -0700