AF网易新闻

#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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 聊天控制器(ChatViewController)界面搭建 14.聊天界面-工具条排版 1)搭建界面 添加聊天控制...
    夜空已沉寂阅读 3,112评论 0 4
  • 1.自定义控件 a.继承某个控件 b.重写initWithFrame方法可以设置一些它的属性 c.在layouts...
    圍繞的城阅读 3,510评论 2 4
  • ViewController.M #import "ViewController.h" #import "AFHT...
    老公123阅读 175评论 0 0
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,161评论 1 32
  • *7月8日上午 N:Block :跟一个函数块差不多,会对里面所有的内容的引用计数+1,想要解决就用__block...
    炙冰阅读 2,562评论 1 14