解析

ViewController.M

#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 ()

@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"];

    //GET 接口

    [managerGET:JSON_URLparameters:nilheaders:nilprogress:^(NSProgress*_NonnulldownloadProgress) {


    }success:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        //数据请求的成功回调

        NSLog(@"////////////%@",responseObject);

        for(NSDictionary* dic  inresponseObject[@"category"]) {


            MyModel* model = [MyModelnew];

            [modelsetValuesForKeysWithDictionary:dic];

            [self.dataSourceaddObject:model];

        }

        ///重要..............

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tableViewreloadData];

        });



    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

        //数据请求的失败回调

        NSLog(@"///////////%@",error);

    }];


}

- (IBAction)POST:(id)sender {

    //POST

    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];

    NSDictionary * dic = @{@"methodName":@"HomeIndex"};


    [managerPOST:PATHparameters:dicheaders:nilprogress:^(NSProgress*_NonnulluploadProgress) {


    }success:^(NSURLSessionDataTask*_Nonnulltask,id  _NullableresponseObject) {

        NSLog(@"%@",responseObject);

    }failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

        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 self.dataSource.count;

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(cell==nil) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];

    }

    if(self.dataSource.count>0) {

        MyModel* model =_dataSource[indexPath.row];

        cell.imageView.image= [UIImageimageNamed:[NSStringstringWithFormat:@"category_%@.jpg",model.categoryName]];

        cell.textLabel.text= model.categoryName;

        cell.detailTextLabel.text= [NSStringstringWithFormat:@"一共有%@款应用,其中%@款限免",model.count,model.lessenPrice];


    }

    returncell;

}

@end

MyModel

#import

@interfaceMyModel :NSObject

@property(nonatomic,strong)NSString* categoryId,*categoryName,*count,*lessenPrice;

@end

你可能感兴趣的:(解析)