iOS开发项目篇—23获取微博数据
一、新浪微博接口
说明:红框框住的两个接口是一样的,只是地址不一样而已,在这里选择的是蓝框选中的接口。
新:代表最新出的一些接口
高:代表高级接口,需要有一定的权限才能使用
返回的数据和返回的字段说明:
说明:返回的是一个JSON数组。
请求参数(发送请求时,必须要传递access_token参数):
二、获取微博数据
首页界面的实现文件:
1 //
2 // YYHomeTableViewController.m
3 //
4
5 #import "YYHomeTableViewController.h"
6 #import "YYOneViewController.h"
7 #import "YYTitleButton.h"
8 #import "YYPopMenu.h"
9 #import "YYAccountModel.h"
10 #import "YYAccountTool.h"
11 #import "AFNetworking.h"
12
13 @interface YYHomeTableViewController ()<YYPopMenuDelegate>
14 @property(nonatomic,assign)BOOL down;
15 @end
16
17 @implementation YYHomeTableViewController
18
19 - (id)initWithStyle:(UITableViewStyle)style
20 {
21 self = [super initWithStyle:style];
22 if (self) {
23 // Custom initialization
24 }
25 return self;
26 }
27
28 - (void)viewDidLoad
29 {
30 [super viewDidLoad];
31
32 //设置导航栏内容
33 [self setupNavBar];
34
35 //加载最新数据
36 [self loadNewStatus];
37
38 }
39
40 /**加载最新微博数据*/
41 -(void)loadNewStatus
42 {
43 //1.获得请求管理者
44 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
45
46 //2.封装请求参数
47
48 NSMutableDictionary *params=[NSMutableDictionary dictionary];
49 params[@"access_token"] =[YYAccountTool accountModel].access_token;
50 //设置请求返回3天数据
51 params[@"count"]=@3;
52
53
54 //3.发送Post请求
55 // url:https://api.weibo.com/2/statuses/home_timeline.json
56 [mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary*accountDict) {
57
58 YYLog(@"请求成功--%@",accountDict);
59 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
60 YYLog(@"请求失败");
61 }];
62
63 }
64 /**设置导航栏内容*/
65 -(void)setupNavBar
66 {
67 self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_friendsearch" highImageName:@"navigationbar_friendsearch_highlighted" target:self action:@selector(friendsearch)];
68 self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_pop" highImageName:@"navigationbar_pop_highlighted" target:self action:@selector(pop)];
69
70 //设置导航栏按钮
71 YYTitleButton *titleButton=[[YYTitleButton alloc]init];
72 //设置文字
73 [titleButton setTitle:@"首页" forState:UIControlStateNormal];
74 //设置图标
75 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
76 //设置背景
77 [titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
78
79 //设置尺寸
80 titleButton.width=100;
81 titleButton.height=35;
82 //监听按钮的点击事件
83 [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
84 self.navigationItem.titleView=titleButton;
85 }
86 -(void)titleButtonClick:(UIButton *)titleButton
87 {
88 // UIImage *titleImage=[UIImage imageWithName:@"navigationbar_arrow_down"];
89 //
90 // if (titleButton.currentImage==titleImage) {
91 //换成箭头向上
92 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
93
94 UITableView *tableView=[[UITableView alloc]init];
95 [tableView setBackgroundColor:[UIColor yellowColor]];
96 YYPopMenu *menu=[YYPopMenu popMenuWithContentView:tableView];
97 [menu showInRect:CGRectMake(60, 55, 200, 200)];
98 menu.dimBackground=YES;
99 // menu.arrowPosition=YYPopMenuArrowPositionLeft;
100 menu.arrowPosition=YYPopMenuArrowPositionRight;
101 menu.delegate=self;
102
103 // }else
104 // {
105 // //换成箭头向下
106 // [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
107 // }
108 }
109
110
111 #pragma mark-YYPopMenuDelegate
112 //弹出菜单
113 -(void)popMenuDidDismissed:(YYPopMenu *)popMenu
114 {
115 YYTitleButton *titleButton=(YYTitleButton *)self.navigationItem.titleView;
116 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
117 }
118 -(void)pop
119 {
120 YYLog(@"---POP---");
121 }
122 -(void)friendsearch
123 {
124 //跳转到one这个子控制器界面
125 YYOneViewController *one=[[YYOneViewController alloc]init];
126 one.title=@"One";
127 //拿到当前控制器
128 [self.navigationController pushViewController:one animated:YES];
129
130 }
131
132 #pragma mark - Table view data source
133 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
134 {
135 return 20;
136 }
137
138 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
139 {
140 static NSString *ID = @"cell";
141 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
142 if (!cell) {
143 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
144 }
145 cell.textLabel.text = [NSString stringWithFormat:@"%d----首页测试数据", indexPath.row];
146 return cell;
147 }
148
149 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
150 {
151 //点击cell的时候,跳到下一个界面
152 UIViewController *newVc = [[UIViewController alloc] init];
153 newVc.view.backgroundColor = [UIColor redColor];
154 newVc.title = @"新控制器";
155 [self.navigationController pushViewController:newVc animated:YES];
156 }
157
158 @end
打印结果:
为了便于查看,导入一个分类,调整为中文显示。
分类中的代码如下:
1 #import <Foundation/Foundation.h>
2
3 @implementation NSDictionary (Log)
4 - (NSString *)descriptionWithLocale:(id)locale
5 {
6 NSMutableString *str = [NSMutableString string];
7
8 [str appendString:@"{\n"];
9
10 // 遍历字典的所有键值对
11 [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
12 [str appendFormat:@"\t%@ = %@,\n", key, obj];
13 }];
14
15 [str appendString:@"}"];
16
17 // 查出最后一个,的范围
18 NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
19 if (range.length) {
20 // 删掉最后一个,
21 [str deleteCharactersInRange:range];
22 }
23
24 return str;
25 }
26 @end
27
28 @implementation NSArray (Log)
29 - (NSString *)descriptionWithLocale:(id)locale
30 {
31 NSMutableString *str = [NSMutableString string];
32
33 [str appendString:@"[\n"];
34
35 // 遍历数组的所有元素
36 [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
37 [str appendFormat:@"%@,\n", obj];
38 }];
39
40 [str appendString:@"]"];
41
42 // 查出最后一个,的范围
43 NSRange range = [str rangeOfString:@"," options:NSBackwardsSearch];
44 if (range.length) {
45 // 删掉最后一个,
46 [str deleteCharactersInRange:range];
47 }
48
49 return str;
50 }
51 @end
注意:数组下表越界问题
打印结果查看:
三、数据处理
1.微博数据的结构
2.导入处理图片的第三方框架,硬盘对硬盘导入图片
3.实现代码
YYHomeTableViewController.m文件代码:
1 //
2 // YYHomeTableViewController.m
3 //
4
5 #import "YYHomeTableViewController.h"
6 #import "YYOneViewController.h"
7 #import "YYTitleButton.h"
8 #import "YYPopMenu.h"
9 #import "YYAccountModel.h"
10 #import "YYAccountTool.h"
11 #import "AFNetworking.h"
12 #import "UIImageView+WebCache.h"
13
14 @interface YYHomeTableViewController ()<YYPopMenuDelegate>
15 @property(nonatomic,assign)BOOL down;
16 @property(nonatomic,strong)NSArray *statuses;
17 @end
18
19 @implementation YYHomeTableViewController
20
21 - (id)initWithStyle:(UITableViewStyle)style
22 {
23 self = [super initWithStyle:style];
24 if (self) {
25 // Custom initialization
26 }
27 return self;
28 }
29
30 - (void)viewDidLoad
31 {
32 [super viewDidLoad];
33
34 //设置导航栏内容
35 [self setupNavBar];
36
37 //加载最新数据
38 [self loadNewStatus];
39
40 }
41
42 /**加载最新微博数据*/
43 -(void)loadNewStatus
44 {
45 //1.获得请求管理者
46 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
47
48 //2.封装请求参数
49
50 NSMutableDictionary *params=[NSMutableDictionary dictionary];
51 params[@"access_token"] =[YYAccountTool accountModel].access_token;
52 //设置请求返回3天数据
53 params[@"count"]=@10;
54
55
56 //3.发送Post请求
57 // url:https://api.weibo.com/2/statuses/home_timeline.json
58 [mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:params success:^(AFHTTPRequestOperation *operation, NSDictionary*accountDict) {
59
60 YYLog(@"请求成功--%@",accountDict);
61 self.statuses=accountDict[@"statuses"];
62
63 //重新刷新表格
64 [self.tableView reloadData];
65 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
66 YYLog(@"请求失败");
67 }];
68
69 }
70 /**设置导航栏内容*/
71 -(void)setupNavBar
72 {
73 self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_friendsearch" highImageName:@"navigationbar_friendsearch_highlighted" target:self action:@selector(friendsearch)];
74 self.navigationItem.rightBarButtonItem=[UIBarButtonItem itemWithImageName:@"navigationbar_pop" highImageName:@"navigationbar_pop_highlighted" target:self action:@selector(pop)];
75
76 //设置导航栏按钮
77 YYTitleButton *titleButton=[[YYTitleButton alloc]init];
78 //设置文字
79 [titleButton setTitle:@"首页" forState:UIControlStateNormal];
80 //设置图标
81 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
82 //设置背景
83 [titleButton setBackgroundImage:[UIImage resizedImage:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
84
85 //设置尺寸
86 titleButton.width=100;
87 titleButton.height=35;
88 //监听按钮的点击事件
89 [titleButton addTarget:self action:@selector(titleButtonClick:) forControlEvents:UIControlEventTouchUpInside];
90 self.navigationItem.titleView=titleButton;
91 }
92 -(void)titleButtonClick:(UIButton *)titleButton
93 {
94
95 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_up"] forState:UIControlStateNormal];
96
97 UITableView *tableView=[[UITableView alloc]init];
98 [tableView setBackgroundColor:[UIColor yellowColor]];
99 YYPopMenu *menu=[YYPopMenu popMenuWithContentView:tableView];
100 [menu showInRect:CGRectMake(60, 55, 200, 200)];
101 menu.dimBackground=YES;
102
103 menu.arrowPosition=YYPopMenuArrowPositionRight;
104 menu.delegate=self;
105 }
106
107
108 #pragma mark-YYPopMenuDelegate
109 //弹出菜单
110 -(void)popMenuDidDismissed:(YYPopMenu *)popMenu
111 {
112 YYTitleButton *titleButton=(YYTitleButton *)self.navigationItem.titleView;
113 [titleButton setImage:[UIImage imageWithName:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
114 }
115 -(void)pop
116 {
117 YYLog(@"---POP---");
118 }
119 -(void)friendsearch
120 {
121 //跳转到one这个子控制器界面
122 YYOneViewController *one=[[YYOneViewController alloc]init];
123 one.title=@"One";
124 //拿到当前控制器
125 [self.navigationController pushViewController:one animated:YES];
126
127 }
128
129 #pragma mark - Table view data source
130 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
131 {
132 return self.statuses.count;
133 }
134
135 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
136 {
137 static NSString *ID = @"cell";
138 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
139 if (!cell) {
140 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
141 }
142 // cell.textLabel.text = [NSString stringWithFormat:@"%d----首页测试数据", indexPath.row];
143
144 //取出这行对应的微博字典数据
145 NSDictionary *statusDict = self.statuses[indexPath.row];
146 cell.textLabel.text=statusDict[@"text"];
147
148 //取出user字典数据
149 NSDictionary *userDict=statusDict[@"user"];
150 cell.detailTextLabel.text=userDict[@"name"];
151
152 //下载头像图片
153 NSString *imageUrlStr=userDict[@"profile_image_url"];
154 [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrlStr] placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
155 return cell;
156 }
157
158 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
159 {
160 //点击cell的时候,跳到下一个界面
161 UIViewController *newVc = [[UIViewController alloc] init];
162 newVc.view.backgroundColor = [UIColor redColor];
163 newVc.title = @"新控制器";
164 [self.navigationController pushViewController:newVc animated:YES];
165 }
166
167 @end
4.运行效果