#import "ViewController.h"
#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 ()<UITableViewDelegate,UITableViewDataSource>
@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 * manager = [AFHTTPSessionManager manager];
//这是关闭它的自动解析功能
//mannage.responseSerializer = [AFHTTPResponseSerializer serializer];
//添加的支持的解析类型@"text/html",application/json
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
//CET 接口
[managerGET:JSON_URLparameters:nilheaders:nilprogress:^(NSProgress*_NonnulldownloadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
//数据请求的成功回调
NSLog(@"///////%@",responseObject);
for(NSDictionary* dicinresponseObject[@"category"]) {
MyModel* model = [MyModelnew];
[modelsetValuesForKeysWithDictionary:dic];
[self.dataSourceaddObject:model];
}
//重要
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//数据请求的失败回调
NSLog(@"////////%@",error);
}];
}
- (IBAction)POST:(id)sender {
//POST
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
NSDictionary * dic = @{@"methodName":@"HomIndex"};
[managerPOST:PATHparameters:nilheaders:nilprogress:^(NSProgress*_NonnulluploadProgress) {
}success:^(NSURLSessionDataTask*_Nonnulltask,id _NullableresponseObject) {
NSLog(@"%@",responseObject);
}failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
- (IBAction)Monitor:(id)sender {
//做一个网络状态监听
AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"%ld",(long)status);
}];
//开始监听
[manager.reachabilityManager startMonitoring];
//停止监听
//[manager.reachabilityManager stopMonitoring];
}
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
return self.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* mondel =_dataSource[indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"category_%@.jpg",mondel.categoryName]];
cell.textLabel.text = mondel.categoryName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"一共有%@款应用,其中%@款限免",mondel.count,mondel.lessenPrice];
}
returncell;
}
@end