AFN用法

version_update_appstore

@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,assignint page;

@property(nonatomic,assignint pages;

@property(nonatomic,copy) NSArray *list;

@end

@interface Model : NSObject

@property(nonatomic,assignint 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 ()

@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

你可能感兴趣的:(AFN用法)