AFNetworking解析

首先导入categorylist文件和 AFNetworking文件
再创建mymodel继承月NSobject
在mymodel.h里面写:@property(nonatomic,strong) NSString *categoryId,*categoryName,*count,*lessenPrice;(后面的属性是解析对应的属性名,自己找)
别忘了开启网络数据
在viewcontroller.m里面写:

先导入头文件
#import"AFHTTPSessionManager.h"
#import"mymodel.h"
#define JSON_URL @“http://iappfree.candou.com:8080/free/categories/free
#define PATH @“http://api.izhangchu.com” // methodName: HomeIndex
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property(nonatomic,strong)NSMutableArray *datasource;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    self.datasource=[NSMutableArray new];
    }

  • (IBAction)GET:(id)sender {
    //创建数据请求管理对象
    AFHTTPSessionManager *mananger=[AFHTTPSessionManager manager];
    //这是关闭它的自动解析功能
    //mannage.responseSerializer = [AFHTTPResponseSerializer serializer];
    //添加的支持的解析类型@“text/html”,
    mananger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@“application/json”];
    //GET 接口
    [mananger GET:JSON_URL parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    //数据请求的成功回调
    NSLog(@"%@",responseObject);
    for (NSDictionary *dic in responseObject[@“category”]) {
    mymodel *model=[[mymodel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    [self.datasource addObject:model];
    }
    //重要的一点
    dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableview reloadData];
    });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //数据请求的失败回调
    NSLog(@"%@",error);
    }];
    }

  • (IBAction)POST:(id)sender {
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    //拼接参数 methodName: HomeIndex
    NSDictionary *dic=@{@“methodName”?“HomeIndex”};
    //POST
    [manager POST:PATH parameters:dic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    NSLog(@"%@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"%@",error);
    }];

}

  • (IBAction)monitor:(id)sender {
    //做一个网络状态监听
    AFHTTPSessionManager manager=[AFHTTPSessionManager manager];
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    /
    AFNetworkReachabilityStatusUnknown = -1,
    AFNetworkReachabilityStatusNotReachable = 0,
    AFNetworkReachabilityStatusReachableViaWWAN = 1,
    AFNetworkReachabilityStatusReachableViaWiFi = 2,*/
    NSLog(@"%ld",(long)status);
    }];
    //开始监听
    [manager.reachabilityManager startMonitoring];
    //关闭监听
    //[manager.reachabilityManager stopMonitoring];
    }

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _datasource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@“cell”];
if (!cell) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“cell”];
}
if (self.datasource.count>0) {
mymodel *model=datasource[indexPath.row];
cell.imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"category
%@.jpg",model.categoryName]];
cell.textLabel.text=model.categoryName;
cell.detailTextLabel.text=[NSString stringWithFormat:@“共有%@款,其中%@款限免”,model.count,model.lessenPrice];
}
return cell;
}
@end

######双层取值:
AFHTTPSessionManager *HttpHandle=[AFHTTPSessionManager manager];
//用post请求数据
[HttpHandle POST:@“http://v.juhe.cn/joke/content/list.php?time=1418745237&key=eaaf69cdca2f46e403a264f5ef7cb74b” parameters:nil headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {

} success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"%@",responseObject);
    //字典数组双层取值
    NSDictionary *alldic=responseObject[@"result"];
    
    NSArray *dicArr = [alldic objectForKey:@"data"];
    
    for (NSDictionary *dic in dicArr) {
        mymodel *mod=[[mymodel alloc]init];
        [mod setValuesForKeysWithDictionary:dic];
        [self.datasource addObject:mod];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tbv reloadData];
    });
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"%@",error);
}];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用AFNetworking库下载文件时,有几种常见的方式。第一种方式是使用AFHTTPRequestOperation进行文件下载,需要传入文件的下载地址URL、自定义的文件名fileName以及下载到的文件路径d_path。 另一种方式是使用AFHTTPSessionManager结合NSURLSessionDownloadTask进行文件下载。同样需要传入文件的下载地址URL和下载到的文件路径d_path。 此外,还可以使用AFNetworking库来实现文件的断点下载功能。通过对AFNetworking3.0版本的学习,你可以了解到如何使用AFNetworking库来实现文件断点下载,并且可以获取一些有价值的参考信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [AFNetworking从指定网址下载文件的两种方式总结](https://blog.csdn.net/u014063717/article/details/52232858)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [iOS利用AFNetworking3.0——实现文件断点下载](https://download.csdn.net/download/weixin_38607784/12786948)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值