AFN  加MJ解析

@interface Model : NSObject

@property(nonatomic , strong)NSString *name;

@property(nonatomic , strong)NSString *start_time;

@property(nonatomic , strong)NSString *create_time;

@property(nonatomic , strong)NSString *describe;

@property(nonatomic , strong)NSString *type;

@end

表格

- (void)setvaluesModel:(Model*)model{

    if(model) {

        self.label1.text= model.name;

        self.label2.text= model.describe;

        self.label3.text= model.start_time;

    }

}


#import "ViewController.h"

#import "AFHTTPSessionManager.h"

#import "TableViewCell.h"

#import "Model.h"

#import "DataManager.h"

#import "Student+CoreDataClass.h"

#import "MJExtension.h"


- (void)Load{

    AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

    [managerGET:@"http://127.0.0.1/first.json"parameters:nilprogress:^(NSProgress*_NonnulldownloadProgress) {


    }success:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        self.array = [Model mj_objectArrayWithKeyValuesArray:responseObject];

        for(NSDictionary*dicinresponseObject) {

            [[DataManagershareManager]insert:dic];

        }

        [self.ojtablereloadData];

    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

        NSLog(@"%@",error);

    }];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    returnarr.count;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    if(section ==0) {

        return1;

    }else{

        returnself.array.count;

    }

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(self.array.count>0) {

        Model*model =self.array[indexPath.row];

        [cellsetvaluesModel:model];

        [cell.Btn addTarget:self action:@selector(StartBtn:)forControlEvents:UIControlEventTouchUpInside];

        [cell.Btn setTitle:@"开始考试" forState:UIControlStateNormal];

        cell.Btn.tag= indexPath.row;

    }

    returncell;

}

- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{

    return 150;

}

- (NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

    returnarr[section];

}

- (void)StartBtn:(UIButton*)btn{

}

- (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{

    Student*model =self.array[indexPath.row];

    [[DataManager shareManager]deleteData:model];

    [self.arrayremoveObject:self.array[indexPath.row]];

    [self.ojtable reloadData];

}


下面这个是POST解析


@interfaceImgModel :NSObject

@property(nonatomic,assign)NSDate *create_time;

@property(nonatomic,copy)NSString *id;

@property(nonatomic,copy)NSString *title;

@end

@interfaceDataModel :NSObject

@property(nonatomic,assign) int page;

@property(nonatomic,assign) int pages;

@property(nonatomic,copy) NSArray *list;

@end

@interface Model : NSObject

@property(nonatomic,assign) int code;

@property(nonatomic,strong) DataModel *data;

@end

#import "Model.h"

@implementation ImgModel

@end

@implementation DataModel

@end

@implementation Model

@end

#import "ViewController.h"

#import "Model.h"

#import "AFNetworking.h"

#import "MJExtension.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)NSArray *arr;

@property(nonatomic,strong)UITableView *tbv;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];



    [self createNav];


    [self.viewaddSubview:self.tbv];


}

-(void)createNav{



    //2.发送GET请求

    /*

     第一个参数:请求路径(不包含参数).NSString

     第二个参数:字典(发送给服务器的数据~参数)

     第三个参数:progress 进度回调

     第四个参数:success 成功回调

     task:请求任务

     responseObject:响应体信息(JSON--->OC对象)

     第五个参数:failure 失败回调

     error:错误信息

     响应头:task.response

     */


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];


    //参数

    NSMutableDictionary *params = [NSMutableDictionary dictionary];

    params[@"method"] = @"app.news.getarticlelist";

    params[@"class_id"] =@"3";

    params[@"_debug"] =@"Y";


    [managerPOST:@"http://test.bit.api.meeboo.cc"parameters:paramsprogress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {


        [DataModel mj_setupObjectClassInArray:^NSDictionary *{

            return@{

                     @"list":@"ImgModel"

                     };

        }];


        Model*model = [Modelmj_objectWithKeyValues:responseObject];


        self.arr= model.data.list;


        [self.tbvreloadData];



        NSLog(@"%@---%@",[responseObjectclass],responseObject);




    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

        NSLog(@"请求失败--%@",error);

    }];



}

-(UITableView *)tbv{

    if(!_tbv) {

        _tbv = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

    }


    _tbv.dataSource = self;

    _tbv.delegate=self;


    return _tbv;


}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return_arr.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];


    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    }


    //cell.textLabel.text = [self.arr[indexPath.row] objectForKey:@"title"];

    ImgModel*mod=self.arr[indexPath.row];

    cell.textLabel.text= mod.title;


    returncell;


}



AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];

    [manager GET:@"http://123.126.40.109:7003/asmr/videos/A1100101.shtml" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {


    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        //解析的是数组

        NSArray *dicArr = responseObject[@"result"];

        self.array = [NSArray yy_modelArrayWithClass:[Model class] json:dicArr];

        //解析是字典

//        Model *model = [Model yy_modelWithJSON:responseObject];

//        for (NSDictionary *dic in responseObject[@"result"]) {

//            Model *model = [Model new];

//            [model setValuesForKeysWithDictionary:dic];

//            [self.array addObject:model];

//        }

        [self.ojtable reloadData];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"%@",error);

    }];

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 概述在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,类似...
    liudhkk阅读 12,993评论 3 38
  • 一、简介 <<UITableView(或简单地说,表视图)的一个实例是用于显示和编辑分层列出的信息的一种手段 <<...
    无邪8阅读 13,687评论 3 3
  • 前言 最近忙完项目比较闲,想写一篇博客来分享一些自学iOS的心得体会,希望对迷茫的你有所帮助。博主非科班出身,一些...
    GitHubPorter阅读 5,346评论 9 5
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,902评论 1 32
  • 六盘天高白云悠, 风尘一日至原州。 欲穷夜暮景色美, 携友同去登古楼。
    魏加恩阅读 2,299评论 0 1

友情链接更多精彩内容