A.新浪获取微博API
1.读取微博API
 
 
2.“statuses/home_timeline”接口
 
 
 
B.在app中获取微博数据
1.在“首页”控制器发送请求,获取json数据
 /** 加载微博数据 */
- (void) loadWeiboData {
// 创建AFNetworking的http操作中管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 设置参数
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token; // 发送请求
[manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
HVWLog(@"获取微博数据成功-------%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HVWLog(@"获取微博数据失败------%@", error);
}];
}
 
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)用户类(包括登陆用户和关注的人)
 
暂时简单封装几个属性,以后再扩展
 //
// HVWUser.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWUser : NSObject /** 友好显示名称 */
@property(nonatomic, copy) NSString *name; /** 用户头像地址(中图),50×50像素 */
@property(nonatomic, copy) NSString *profile_image_url; +(instancetype) userWithDict:(NSDictionary *) dict; @end
 
 //
// HVWUser.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWUser.h" @implementation HVWUser +(instancetype) userWithDict:(NSDictionary *) dict {
HVWUser *user = [[self alloc] init]; user.name = dict[@"name"];
user.profile_image_url = dict[@"profile_image_url"]; return user;
} @end
 
“微博”类
 //
// HVWStatus.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h>
#import "HVWUser.h" @interface HVWStatus : NSObject /** 微博信息内容 */
@property(nonatomic, copy) NSString *text; /** 微博作者的用户信息字段 详细 */
@property(nonatomic, strong) HVWUser *user; /** 微博配图地址数组,里面装载的时HVWPic模型 */
@property(nonatomic, strong) NSArray *pic_urls; + (instancetype) statusWithDict:(NSDictionary *) dict; @end
 
 //
// HVWStatus.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWStatus.h"
#import "HVWUser.h" @implementation HVWStatus + (instancetype) statusWithDict:(NSDictionary *) dict {
HVWStatus *status = [[HVWStatus alloc] init]; status.text = dict[@"text"];
status.user = [HVWUser userWithDict:dict[@"user"]]; return status;
} @end
 
3.在“首页”中显示简单的微博信息
使用SDWebImage加载网络图片
 
 //  HVWHomeViewController.m
/** 加载微博数据 */
- (void) loadWeiboData {
// 创建AFNetworking的http操作中管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 设置参数
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token; // 发送请求
[manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
// HVWLog(@"获取微博数据成功-------%@", responseObject); // 保存数据到内存
NSArray *dataArray = responseObject[@"statuses"]; for (NSDictionary *dict in dataArray) {
HVWStatus *status = [HVWStatus statusWithDict:dict];
[self.statuses addObject:status];
} // 刷新数据
[self.tableView reloadData]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HVWLog(@"获取微博数据失败------%@", error);
}];
} - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *ID = @"HomeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} HVWStatus *status = self.statuses[indexPath.row];
HVWUser *user = status.user; // 加载内容
cell.textLabel.text = status.text;
// 作者
cell.detailTextLabel.text = user.name;
// 作者头像
NSString *imageUrlStr = user.profile_image_url;
[cell.imageView setImageWithURL:[NSURL URLWithString:imageUrlStr] placeholderImage:[UIImage imageWithNamed:@"avatar_default_small"]]; return cell;
}
 
 
 
B.使用第三方框架转换json字典数据到模型
     因为返回的json字典数据量大层多,自己编写的代码运行效率可能比较低下,这里使用李明杰老师的MJExtension框架来进行转换
 
1.使用此框架,只需要相应的类和成员属性,不用自己编写初始化方法
 //
// HVWUser.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWUser : NSObject /** 友好显示名称 */
@property(nonatomic, copy) NSString *name; /** 用户头像地址(中图),50×50像素 */
@property(nonatomic, copy) NSString *profile_image_url; @end
 
 //
// HVWStatus.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h>
#import "HVWUser.h" @interface HVWStatus : NSObject /** 微博信息内容 */
@property(nonatomic, copy) NSString *text; /** 微博作者的用户信息字段 详细 */
@property(nonatomic, strong) HVWUser *user; /** 微博配图地址数组,里面装载的时HVWPic模型 */
@property(nonatomic, strong) NSArray *pic_urls; @end
 
 //  HVWHomeViewController.m
/** 加载微博数据 */
- (void) loadWeiboData {
// 创建AFNetworking的http操作中管理器
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; // 设置参数
NSMutableDictionary *param = [NSMutableDictionary dictionary];
param[@"access_token"] = [HVWAccountInfoTool accountInfo].access_token; // 发送请求
[manager GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, NSDictionary *responseObject) {
// HVWLog(@"获取微博数据成功-------%@", responseObject); // 保存数据到内存
NSArray *dataArray = responseObject[@"statuses"]; // 使用MJExtension直接进行字典-模型转换
self.statuses = [HVWStatus objectArrayWithKeyValuesArray:dataArray]; // 刷新数据
[self.tableView reloadData]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
HVWLog(@"获取微博数据失败------%@", error);
}];
}
 
运行成功!
 
 
2.指定数组元素包装类,可以在代码中指定用什么类来包装一个数组中的数据
例如,返回的数据中,有"pic_urls"的数组,里面存放的是所有的微博配图
 
 
没有配置包装类的时候,返回的就是一个字典,不会被自动封装
 
创建一个"配图”类
 //
// HVWPic.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWPic : NSObject /** 缩略图片地址,没有时不返回此字段 */
@property(nonatomic, copy) NSString *thumbnail_pic; @end
 
“微博”类中已经有了对这个数组的映射,但是不会自动把里面的数据自动转换成HVWPic
 
所以,需要实现一个方法来指定数组子元素的包装类:
 //
// HVWStatus.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/5.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWStatus.h"
#import "HVWPic.h" // 注意引入框架
#import "MJExtension.h" @implementation HVWStatus - (NSDictionary *)objectClassInArray {
// 返回一个字典,创建数组子元素和包装类的映射关系
return @{@"pic_urls": [HVWPic class]};
} @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. EF CodeFirst-----简单demo示例

    关于EF CodeFirst的文章院子里有很多的学习资料,但大多数都是一些讲Model通过特性或是Fluent API与数据库之间形成映射的关系,看了相关的文章之后,Model如何映射到数据还是有些迷 ...

  2. HDU (线段树 单点更新) I Hate It

    和上一道题没什么变化,只不过把单点增减变成了单点替换,把区间求和变成了区间求最大值. #include <cstdio> #include <algorithm> using ...

  3. UVa (二分) 11627 Slalom

    题意: 有宽度相同的水平的n个旗门,水平(纵坐标严格递增)滑行的最大速度为Vh(水平速度可以任意调节).然后还有S双滑雪板,每双滑雪板的垂直速度一定. 然后求能通过的滑板鞋的最大速度. 分析: 显然, ...

  4. 51nod1627 瞬间移动

    打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... //打表可以看出来是组合数...妈呀为什么弄成n+m-4,n-1,m-3就错啊... #include<cstd ...

  5. phpstorm10.0.1和webstorm11注册

    webstorm11 for win 注册时选择“License server”输入“http://15.idea.lanyus.com/”点击“OK” phpstorm10.0.1 for win ...

  6. Linux编程(获取系统时间)

    #include <stdio.h> #include <time.h> int main() { time_t now; struct tm *w; time(&no ...

  7. 深入理解require/include的顺序

    在大型的Web项目中, include_path是一个模块化设计的根本中的根本(当然,现在也有很多基于autoload的设计, 这个不影响本文的探讨), 但是正是因为include_path, 经常会 ...

  8. Java之路

    Java程序员 高级特性 反射.泛型.注释符.自动装箱和拆箱.枚举类.可变参数.可变返回类型.增强循环.静态导入 核心编程 IO.多线程.实体类.集合类.正则表达式.XML和属性文件 图形编程 AWT ...

  9. 【转】使用Python的IDE:Eclipse+PyDev

    原文网址:http://www.crifan.com/try_with_python_ide_eclipse_pydev/ 之前已经介绍过了一些基本知识: [整理][多图详解]如何在Windows下开 ...

  10. 大数据时代的技术hive:hive介绍

    我最近研究了hive的相关技术,有点心得,这里和大家分享下. 首先我们要知道hive到底是做什么的.下面这几段文字很好的描述了hive的特性: 1.hive是基于Hadoop的一个数据仓库工具,可以将 ...