网络请求

#import@interface DataBase : NSObject

{

NSDictionary *dict;

}

+ (instancetype)shareData;

- (void)getURL;


//

//  DataBase.m

//  项目一月考技能

//

//  Created by hh on 2017/11/23.

//  Copyright © 2017年 www.baidu.com. All rights reserved.

//

#import "DataBase.h"

#import "AFNetworking.h"

#import "JSONKit.h"

#define Monthly_Examination @"http://api.jisuapi.com/news/get?channel=头条&start=0&num=10&appkey=de394933e1a3e2db"

static DataBase *db = nil;

@implementation DataBase

+ (instancetype)shareData

{

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

db = [[DataBase alloc]init];

});

return db;

}

+ (instancetype)allocWithZone:(struct _NSZone *)zone

{

if (!db)

{

db = [super allocWithZone:zone];

}

return db;

}

- (id)mutableCopy

{

return self;

}

- (id)copy

{

return self;

}

- (void)getURL

{

dict = [[NSDictionary alloc]init];

NSString *strURl = [Monthly_Examination stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];

manger.responseSerializer = [[AFHTTPResponseSerializer alloc]init];

[manger GET:strURl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"成功");

dict = [responseObject objectFromJSONData];

[[NSNotificationCenter defaultCenter]postNotificationName:@"huxiaobo" object:dict];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

NSLog(@"失败");

}];

}

@end

//// FirstViewController.m// 项目一月考技能//// Created by hh on 2017/11/23.// Copyright © 2017年 www.baidu.com. All rights reserved.//#import "FirstViewController.h"#import "DataBase.h"#import "FffffViewController.h"@interface FirstViewController (){

// 全局变量

NSDictionary *dicts;

NSArray *arrays;

}

// 属性  表格;

@property (nonatomic,strong)UITableView *tables;

@end

@implementation FirstViewController

- (void)viewDidLoad {

[super viewDidLoad];

// 页面的背景颜色及导航栏的标题、背景颜色

[self setBackgroundColor];

// 类方法将DataBase类里的getURL正此控制器实现以及通知传值(接收)

[self getUrlwithdata];

// 表格的创建

[self tables];

}

#pragma mark - 页面的背景颜色及导航栏的标题、背景颜色;

- (void)setBackgroundColor

{

self.view.backgroundColor = [UIColor whiteColor];

self.navigationItem.title = @"头条";

[self.navigationController.navigationBar setBarTintColor:[UIColor blackColor]];

[self.navigationController.navigationBar setTitleTextAttributes:

@{NSFontAttributeName:[UIFont systemFontOfSize:20 weight:1.5],

NSForegroundColorAttributeName:[UIColor whiteColor]}];

}

#pragma mark - 类方法将DataBase类里的getURL正此控制器实现以及通知传值(接收);

- (void)getUrlwithdata

{

[[DataBase shareData]getURL];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(TransferData:) name:@"huxiaobo" object:nil];

}

#pragma mark - 表格的创建 位置及大小 和设置代理;

- (UITableView *)tables

{

if (!_tables)

{

_tables = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];

_tables.delegate =self;

_tables.dataSource = self;

[self.view addSubview:_tables];

}

return _tables;

}

#pragma mark - 表格的代理及数据源方法

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

{

return arrays.count;

}

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

{

static NSString *cellS = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellS];

if (!cell)

{

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellS];

}

cell.textLabel.text = [[arrays objectAtIndex:indexPath.row] objectForKey:@"title"];

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[arrays objectAtIndex:2] objectForKey:@"pic"]]];

//image与data的相互转换

UIImage *image = [UIImage imageWithData:data];

//配置图片到imageView上

cell.imageView.image = image;

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

FffffViewController *fff = [[FffffViewController alloc]init];

fff.WebUrl = [[arrays objectAtIndex:indexPath.row] objectForKey:@"weburl"];

[self.navigationController pushViewController:fff animated:YES];

}

#pragma mark - 通知传值附加的事件;

- (void)TransferData:(NSNotification *)notifi

{

arrays = [[NSArray alloc]init];

dicts = [[NSDictionary alloc]init];

dicts = notifi.object;

arrays = [[dicts objectForKey:@"result"] objectForKey:@"list"];

NSLog(@"arrays:::::%@",arrays);

[self.tables reloadData];

}

@end

//// FffffViewController.h// 项目一月考技能//// Created by hh on 2017/11/23.// Copyright © 2017年 www.baidu.com. All rights reserved.//#import@interface FffffViewController : UIViewController

@property (nonatomic,strong)NSString *WebUrl;

@end


//

//  FffffViewController.m

//  项目一月考技能

//

//  Created by hh on 2017/11/23.

//  Copyright © 2017年 www.baidu.com. All rights reserved.

//

#import "FffffViewController.h"

@interface FffffViewController ()

@end

@implementation FffffViewController

- (void)viewDidLoad {

[super viewDidLoad];

//页面背景颜色

self.view.backgroundColor = [UIColor whiteColor];

//网页视图大小和位置

UIWebView* webView = [[UIWebView alloc]initWithFrame:self.view.frame];

//为url添加内容

NSURL *url = [NSURL URLWithString:self.WebUrl];//创建URL

//请求

NSURLRequest* request = [NSURLRequest requestWithURL:url];

//加载

[webView loadRequest:request];

//将web视图添加到视图上

[self.view addSubview:webView];

}

@end

你可能感兴趣的:(网络请求)