A.新浪获取微博API
1.读取微博API
 
 
2.“statuses/home_timeline”接口
 
 
 
B.在app中获取微博数据
1.在“首页”控制器发送请求,获取json数据
  1. /** 加载微博数据 */
  2. - (void) loadWeiboData {
  3. // 创建AFNetworking的http操作中管理器
  4. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  5.  
  6. // 设置参数
  7. NSMutableDictionary *param = [NSMutableDictionary dictionary];
  8. param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token;
  9.  
  10. // 发送请求
  11. [manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
  12. HVWLog(@"获取微博数据成功-------%@", responseObject);
  13. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  14. HVWLog(@"获取微博数据失败------%@", error);
  15. }];
  16. }
 
Output:
获取微博数据成功-------{
statuses = [
{
rid = 0_0_2669621413509583897,
visible = {
type = 0,
list_id = 0
},
original_pic = http://ww1.sinaimg.cn/large/c3ad47bejw1eoygflrel2g201w034q34.gif,
mid = 3806890389487492,
source = <a href="http://app.weibo.com/t/feed/3j6BDx" rel="nofollow">
孔明社交管理</a>,
truncated = 0,
reposts_count = 2,
bmiddle_pic = http://ww1.sinaimg.cn/bmiddle/c3ad47bejw1eoygflrel2g201w034q34.gif,
darwin_tags = [
],
....
 
2.封装“用户”、“微博”类,用来装载每条微博信息
 
(1)用户类(包括登陆用户和关注的人)
 
暂时简单封装几个属性,以后再扩展
  1. //
  2. // HVWUser.h
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface HVWUser : NSObject
  12.  
  13. /** 友好显示名称 */
  14. @property(nonatomic, copy) NSString *name;
  15.  
  16. /** 用户头像地址(中图),50×50像素 */
  17. @property(nonatomic, copy) NSString *profile_image_url;
  18.  
  19. +(instancetype) userWithDict:(NSDictionary *) dict;
  20.  
  21. @end
 
  1. //
  2. // HVWUser.m
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "HVWUser.h"
  10.  
  11. @implementation HVWUser
  12.  
  13. +(instancetype) userWithDict:(NSDictionary *) dict {
  14. HVWUser *user = [[self alloc] init];
  15.  
  16. user.name = dict[@"name"];
  17. user.profile_image_url = dict[@"profile_image_url"];
  18.  
  19. return user;
  20. }
  21.  
  22. @end
 
“微博”类
  1. //
  2. // HVWStatus.h
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "HVWUser.h"
  11.  
  12. @interface HVWStatus : NSObject
  13.  
  14. /** 微博信息内容 */
  15. @property(nonatomic, copy) NSString *text;
  16.  
  17. /** 微博作者的用户信息字段 详细 */
  18. @property(nonatomic, strong) HVWUser *user;
  19.  
  20. /** 微博配图地址数组,里面装载的时HVWPic模型 */
  21. @property(nonatomic, strong) NSArray *pic_urls;
  22.  
  23. + (instancetype) statusWithDict:(NSDictionary *) dict;
  24.  
  25. @end
 
  1. //
  2. // HVWStatus.m
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "HVWStatus.h"
  10. #import "HVWUser.h"
  11.  
  12. @implementation HVWStatus
  13.  
  14. + (instancetype) statusWithDict:(NSDictionary *) dict {
  15. HVWStatus *status = [[HVWStatus alloc] init];
  16.  
  17. status.text = dict[@"text"];
  18. status.user = [HVWUser userWithDict:dict[@"user"]];
  19.  
  20. return status;
  21. }
  22.  
  23. @end
 
3.在“首页”中显示简单的微博信息
使用SDWebImage加载网络图片
 
  1. // HVWHomeViewController.m
  2. /** 加载微博数据 */
  3. - (void) loadWeiboData {
  4. // 创建AFNetworking的http操作中管理器
  5. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  6.  
  7. // 设置参数
  8. NSMutableDictionary *param = [NSMutableDictionary dictionary];
  9. param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token;
  10.  
  11. // 发送请求
  12. [manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
  13. // HVWLog(@"获取微博数据成功-------%@", responseObject);
  14.  
  15. // 保存数据到内存
  16. NSArray *dataArray = responseObject[@"statuses"];
  17.  
  18. for (NSDictionary *dict in dataArray) {
  19. HVWStatus *status = [HVWStatus statusWithDict:dict];
  20. [self.statuses addObject:status];
  21. }
  22.  
  23. // 刷新数据
  24. [self.tableView reloadData];
  25.  
  26. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  27. HVWLog(@"获取微博数据失败------%@", error);
  28. }];
  29. }
  30.  
  31. - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  32.  
  33. static NSString *ID = @"HomeCell";
  34. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
  35. if (nil == cell) {
  36. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
  37. }
  38.  
  39. HVWStatus *status = self.statuses[indexPath.row];
  40. HVWUser *user = status.user;
  41.  
  42. // 加载内容
  43. cell.textLabel.text = status.text;
  44. // 作者
  45. cell.detailTextLabel.text = user.name;
  46. // 作者头像
  47. NSString *imageUrlStr = user.profile_image_url;
  48. [cell.imageView setImageWithURL:[NSURL URLWithString:imageUrlStr] placeholderImage:[UIImage imageWithNamed:@"avatar_default_small"]];
  49.  
  50. return cell;
  51. }
 
 
 
B.使用第三方框架转换json字典数据到模型
     因为返回的json字典数据量大层多,自己编写的代码运行效率可能比较低下,这里使用李明杰老师的MJExtension框架来进行转换
 
1.使用此框架,只需要相应的类和成员属性,不用自己编写初始化方法
  1. //
  2. // HVWUser.h
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface HVWUser : NSObject
  12.  
  13. /** 友好显示名称 */
  14. @property(nonatomic, copy) NSString *name;
  15.  
  16. /** 用户头像地址(中图),50×50像素 */
  17. @property(nonatomic, copy) NSString *profile_image_url;
  18.  
  19. @end
 
  1. //
  2. // HVWStatus.h
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import "HVWUser.h"
  11.  
  12. @interface HVWStatus : NSObject
  13.  
  14. /** 微博信息内容 */
  15. @property(nonatomic, copy) NSString *text;
  16.  
  17. /** 微博作者的用户信息字段 详细 */
  18. @property(nonatomic, strong) HVWUser *user;
  19.  
  20. /** 微博配图地址数组,里面装载的时HVWPic模型 */
  21. @property(nonatomic, strong) NSArray *pic_urls;
  22.  
  23. @end
 
  1. // HVWHomeViewController.m
  2. /** 加载微博数据 */
  3. - (void) loadWeiboData {
  4. // 创建AFNetworking的http操作中管理器
  5. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  6.  
  7. // 设置参数
  8. NSMutableDictionary *param = [NSMutableDictionary dictionary];
  9. param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token;
  10.  
  11. // 发送请求
  12. [manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
  13. // HVWLog(@"获取微博数据成功-------%@", responseObject);
  14.  
  15. // 保存数据到内存
  16. NSArray *dataArray = responseObject[@"statuses"];
  17.  
  18. // 使用MJExtension直接进行字典-模型转换
  19. self.statuses = [HVWStatus objectArrayWithKeyValuesArray:dataArray];
  20.  
  21. // 刷新数据
  22. [self.tableView reloadData];
  23.  
  24. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  25. HVWLog(@"获取微博数据失败------%@", error);
  26. }];
  27. }
 
运行成功!
 
 
2.指定数组元素包装类,可以在代码中指定用什么类来包装一个数组中的数据
例如,返回的数据中,有"pic_urls"的数组,里面存放的是所有的微博配图
 
 
没有配置包装类的时候,返回的就是一个字典,不会被自动封装
 
创建一个"配图”类
  1. //
  2. // HVWPic.h
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10.  
  11. @interface HVWPic : NSObject
  12.  
  13. /** 缩略图片地址,没有时不返回此字段 */
  14. @property(nonatomic, copy) NSString *thumbnail_pic;
  15.  
  16. @end
 
“微博”类中已经有了对这个数组的映射,但是不会自动把里面的数据自动转换成HVWPic
 
所以,需要实现一个方法来指定数组子元素的包装类:
  1. //
  2. // HVWStatus.m
  3. // HVWWeibo
  4. //
  5. // Created by hellovoidworld on 15/2/5.
  6. // Copyright (c) 2015年 hellovoidworld. All rights reserved.
  7. //
  8.  
  9. #import "HVWStatus.h"
  10. #import "HVWPic.h"
  11.  
  12. // 注意引入框架
  13. #import "MJExtension.h"
  14.  
  15. @implementation HVWStatus
  16.  
  17. - (NSDictionary *)objectClassInArray {
  18. // 返回一个字典,创建数组子元素和包装类的映射关系
  19. return @{@"pic_urls": [HVWPic class]};
  20. }
  21.  
  22. @end
 
运行,确认status内的pic_urls数组的元素类型是HVWPic:
 
 
 

[iOS微博项目 - 2.6] - 获取微博数据的更多相关文章

  1. [iOS微博项目 - 3.6] - 获取未读消息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取登陆用户未读消息 1.需求 获取所有未读消息,包括新微博.私信.@.转发.关注等 把未 ...

  2. [iOS微博项目 - 3.2] - 发送微博

    github: https://github.com/hellovoidworld/HVWWeibo   A.使用微博API发送微博 1.需求 学习发送微博API 发送文字微博 发送带有图片的微博   ...

  3. [iOS微博项目 - 3.4] - 获取用户信息

    github: https://github.com/hellovoidworld/HVWWeibo   A.获取用户信息 1.需求 获取用户信息并储存 把用户昵称显示在“首页”界面导航栏的标题上   ...

  4. [iOS微博项目 - 3.1] - 发微博界面

    github: https://github.com/hellovoidworld/HVWWeibo   A.发微博界面:自定义UITextView 1.需求 用UITextView做一个编写微博的输 ...

  5. [iOS微博项目 - 4.0] - 自定义微博cell

    github: https://github.com/hellovoidworld/HVWWeibo A.自定义微博cell基本结构 1.需求 创建自定义cell的雏形 cell包含:内容.工具条 内 ...

  6. AJ学IOS 之微博项目实战(13)发送微博调用相机里面的图片以及调用相机

    AJ分享,必须精品 一:效果 二:代码 相机部分就简单多了,几行代码调用而已,但是如果你要是想实现更多丰富的功能,需要自己写.利用AssetsLibrary.framework,利用这个框架可以获得手 ...

  7. AJ学IOS 之微博项目实战(12)发送微博自定义工具条代理实现点击事件

    AJ分享,必须精品 一:效果 二:封装好的工具条 NYComposeToolbar.h 带代理方法 #import <UIKit/UIKit.h> typedef enum { NYCom ...

  8. AJ学IOS 之微博项目实战(11)发送微博自定义TextView实现带占位文字

    AJ分享,必须精品 一:效果 二:代码: 由于系统自带的UITextField:和UITextView:不能满足我们的需求,所以我们需要自己设计一个. UITextField: 1.文字永远是一行,不 ...

  9. 项目中对获取的数据进行下载成Excel表格

    //moment是操作日期的插件  //引入lodash是为了方便操作数据 //xlsx是获取表格的必须插件   import moment from 'moment'; import _ from  ...

随机推荐

  1. asp.net,CSS设置<TableListView>的title居左,居左,居上

    居左 DIV.TableTitleStyle TABLE.grid TH { text-align:left; } 引用 <div class="TableTitleStyle&quo ...

  2. BZOJ 1787 紧急集合

    LCA.注意细节. #include<iostream> #include<cstdio> #include<cstring> #include<algori ...

  3. LeetCode Valid Number 有效数字(有限自动机)

    题意:判断一个字符串是否是一个合法的数字,包括正负浮点数和整形. 思路:有限自动机可以做,画个图再写程序就可以解决啦,只是实现起来代码的长短而已. 下面取巧来解决,分情况讨论: (1)整数 (2)浮点 ...

  4. HDU5463 Clarke and minecraft

    解题思路:此题刚开始,觉得好繁琐,好混乱,理清思路后,发现很简单.   具体见代码分析. #include<cstdio> #include<cstring> #include ...

  5. 【解题报告】HDU -1142 A Walk Through the Forest

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1142 题目大意:Jimmy要从办公室走路回家,办公室在森林的一侧,家在另一侧,他每天要采取不一样的路线 ...

  6. UIColor,CGColor,CIColor三者的区别和联系

    UIColor,CGColor,CIColor三者的区别和联系((转)) 最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清 ...

  7. IOS设计模式之四(备忘录模式,命令模式)

    本文原文请见:http://www.raywenderlich.com/46988/ios-design-patterns. 由 @krq_tiger(http://weibo.com/xmuzyq) ...

  8. 获取当前匹配元素 包括自身的html

    $(".test").prop("outerHTML"); 来自为知笔记(Wiz)

  9. UITableViewCell 自定义绘制 性能高

    // //  FoodListTableViewCellB.m //  BabyFood // //  Created by zhuang chaoxiao on 16/3/7. //  Copyri ...

  10. 使用Junit实现批量运行

    我们知道使用testng可以很方便的实现批量运行,但有的同学更习惯用junit,那么junit是否也有批量运行的功能呢?答案是 YES,而且有2种方法: 第一种, 选择要测试的包(注意是包而不是文件) ...