您的位置:首页 > 理论基础 > 计算机网络

自己对网络请求进行封装,block作参数

2015-10-06 09:19 537 查看
//
//  RootViewController.m
//  MVC

//

//  Created by  on 15/8/5.

//  Copyright (c) 2015年 Congwang.
All rights reserved.
//

#import "RootViewController.h"

#import "HomePageTableView.h"

#import "NetWorkingEngine.h"

#import "ContentModel.h"
@interface RootViewController ()<</span>UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)HomePageTableView *homePageViewTV;
@property (nonatomic, strong)NSMutableArray *dataSource;
@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //初始化数组

    self.dataSource =
[NSMutableArray array];

    self.homePageViewTV =
[[HomePageTableView alloc]initWithFrame:[UIScreen mainScreen].boundsstyle:UITableViewStylePlain];

    self.homePageViewTV.delegate = self;

    self.homePageViewTV.dataSource = self;

    //注册cell

    [self.homePageViewTV registerClass:[UITableViewCell class]forCellReuseIdentifier:@"cell"];

    //添加视图

    [self.view addSubview:self.homePageViewTV];

    [[NetWorkingEngine shardNetWorkingEngine]getInfoFromServerWithUrlStr:@"http://m2.qiushibaike.com/article/list/text?count=30&page=" withSuccess:^(NSData *response){

        NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];

        NSArray *itemsArray = dic[@"items"];

        NSLog(@"%@",itemsArray);

        for (NSDictionary *userDic in itemsArray)
{

            ContentModel *model = [[ContentModel alloc]initWithDic:userDic];

            [self.dataSource addObject:model];

        }//tableView刷新

        [self.homePageViewTV reloadData];

    } withFailure:^(NSData *error)
{

  

            NSLog(@"哈哈,网速不给力");

      

            }];

        // Do any additional setup after loading the view.

}
#pragma mark -- UITableViewDataSource的方法
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section{

    return self.dataSource.count;

}

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

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

   

    ContentModel *model
= self.dataSource[indexPath.row];

    cell.textLabel.text =
model.contentStr;

    cell.textLabel.numberOfLines = 0;

    return cell;

   

}

- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

NetWorkingEngine.h

//
//  NetWorkingEngine.h
//  MVC
//
//  Created by lanouhn on 15/8/5.
//  Copyright (c) 2015年 Congwang.
All rights reserved.
//

#import 

@interface NetWorkingEngine : NSObject

+ (NetWorkingEngine *)shardNetWorkingEngine;

//请求数据的方法

//block做参数:(void
(^)(NSData *response))block的类型

- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr
withSuccess:(void (^)(NSData *response))success
withFailure:(void (^)(NSError *error))failure;

@end

NetWorkingEngine.m

//
//  NetWorkingEngine.m
//  MVC

//

//  Created by  on 15/8/5.

//  Copyright
(c) 2015年 Congwang. All rights
reserved.
//

#import "NetWorkingEngine.h"

@implementation NetWorkingEngine
//单例方法的实现

+ (NetWorkingEngine *)shardNetWorkingEngine{

    static NetWorkingEngine *netWorkEngine
= nil;

    //GCD - 单例写法

    //在每个线程下都执行一次

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        netWorkEngine = [[NetWorkingEngine alloc] init];

    });

    return netWorkEngine;

   

}

//请求数据的方法 -- 封装系统的请求数据的方法

- (void)getInfoFromServerWithUrlStr:(NSString *)urlStr
withSuccess:(void (^)(NSData *))success
withFailure:(void (^)(NSError *))failure{

    //
1.创建url对象

    NSURL *url = [NSURL URLWithString:urlStr];

    //2.创建请求对象

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //3.通过connection发起请求

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse*response, NSData *data, NSError *connectionError)
{

        //执行到此, 说明请求结束

        if (data) {

            //调用block

            success(data);

           

       

        }

        failure(error);

       

    }];

   

   

}
@end

ContentModel.h

//
//  ContentModel.h
//  MVC

//

//  Created by  on 15/8/5.

//  Copyright
(c) 2015年 Congwang. All rights
reserved.
//

#import 

@interface ContentModel : NSObject
// 内容属性
@property (nonatomic, copy)NSString *contentStr;

- (id)initWithDic:(NSDictionary *)dic;

@end

ContentModel.m

//
//  ContentModel.m
//  MVC

//

//  Created by  on 15/8/5.

//  Copyright
(c) 2015年 Congwang. All rights
reserved.
//

#import "ContentModel.h"

@implementation ContentModel

-(id)initWithDic:(NSDictionary *)dic{

    if (self =
[super init])
{

        self.contentStr =
dic[@"content"];

    }return self;

 }
@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: