一 , 打开网络请求
info.plist
App Transport Security Settings
Allow Arbitrary Loads
二 , 倒入AFNetworking第三方
三 , 创建Model类 继承于nsobject
Model内 .h代码为
#import <Foundation/Foundation.h>
@interface MyModel : NSObject
@property(nonatomic,strong) NSString * categoryId,*categoryName,*count,*lessenPrice;
@end
NSString为接口内的名称
四 , 拖拽Main.storyboard基本为:
然后添加到ViewController.m
五 , ViewController.m内代码为
#import "ViewController.h"
#import "AFNetworking.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 *dataSoure;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.dataSoure = [NSMutableArray new];
}
- (IBAction)GET:(id)sender {
//创建数据请求管理对象
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//这是关闭它的自动解析功能
//mannage.responseSerializer = [AFHTTPResponseSerializer serializer];
//添加的支持的解析类型@"text/html",
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
//GET 接口
[manager 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 new];
[model setValuesForKeysWithDictionary:dic];
[self.dataSoure 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];
//拼接参数
NSDictionary *dic = @{@"methodName":@"Homeindex"};
[manager POST:PATH parameters:dic headers:nil progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
for (NSDictionary *dic in responseObject[@"category"]) {
MyModel *model = [MyModel new];
[model setValuesForKeysWithDictionary:dic];
[self.dataSoure addObject:model];
}
//重要的一点 主线程
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
- (IBAction)Moniter:(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];
//停止监听
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataSoure.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
if (self.dataSoure.count > 0 ){
MyModel *model = _dataSoure[indexPath.row];
cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"category_%@.ipg",model.categoryName]];
cell.textLabel.text = model.categoryName;
cell.detailTextLabel.text = [NSString stringWithFormat:@"一共有%@款应用 , 其中%@款限免",model.count,model.lessenPrice];
}
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end