首先导入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);
}];
}
-(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);
}];