A.封装微博业务
1.需求
把微博相关业务(读取、写微博)
界面控制器不需要知道微博操作细节(例如选择从网络读取还是缓存读取)
 
2.实现
把微博操作封装成一个工具类
把微博网络请求的参数和返回结果也封装成一个类
 
 
 
3.实现
(1)基础参数类
由于多数请求都需要access_token,所以封装一个参数父类
 //
// HVWBaseParam.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWBaseParam : NSObject /** 访问令牌 */
@property(nonatomic, copy) NSString *access_token; @end
 
 //
// HVWBaseParam.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWBaseParam.h"
#import "HVWAccountInfo.h"
#import "HVWAccountInfoTool.h" @implementation HVWBaseParam - (NSString *)access_token {
if (nil == _access_token) {
_access_token = [HVWAccountInfoTool accountInfo].access_token;
}
return _access_token;
} @end
 
(2)首页获取微博
a.参数类
根据微博API请求参数列表
 
 //
// HVWHomeStatusParam.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWBaseParam.h" @interface HVWHomeStatusParam : HVWBaseParam /** false string 采用OAuth授权方式不需要此参数,其他授权方式为必填参数,数值为应用的AppKey。 */
@property(nonatomic, copy) NSString *source; /** false int64 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0。 */
@property(nonatomic, strong) NSNumber *since_id; /** false int64 若指定此参数,则返回ID小于或等于max_id的微博,默认为0。 */
@property(nonatomic, strong) NSNumber *max_id; /** false int 单页返回的记录条数,最大不超过100,默认为20。 */
@property(nonatomic, strong) NSNumber *count; /** false int 返回结果的页码,默认为1。 */
@property(nonatomic, strong) NSNumber *page; /** false int 是否只获取当前应用的数据。0为否(所有数据),1为是(仅当前应用),默认为0。 */
@property(nonatomic, strong) NSNumber *base_app; /** false int 过滤类型ID,0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。 */
@property(nonatomic, strong) NSNumber *feature; /** false int 返回值中user字段开关,0:返回完整user字段、1:user字段仅返回user_id,默认为0。 */
@property(nonatomic, strong) NSNumber *trim_user; @end
 
b.返回结果类
微博返回结果是N条微博数据的数组
(这里在返回结果包装类中直接使用之前创建的HVWStatus类来封装微博数据)
 
每个status里面的元素:
 
 //
// HVWHomeStatusResult.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h>
#import "HVWStatus.h" @interface HVWHomeStatusResult : NSObject /** 微博数组,里面装的HVWStatus模型 */
@property(nonatomic, strong) NSArray *statuses; /** 近期微博总数 */
@property(nonatomic, assign) int total_number; @end
 
 //
// HVWHomeStatusResult.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWHomeStatusResult.h"
#import "MJExtension.h" @implementation HVWHomeStatusResult /** 指明将json数组元素转为什么模型类 */
- (NSDictionary *)objectClassInArray {
return @{@"statuses":[HVWStatus class]};
} @end
 
c.公共请求工具类
内部处理模型转字典的逻辑
 //
// HVWBaseTool.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/10.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface HVWBaseTool : NSObject /** GET请求 */
+ (void) getWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure; /** POST请求(不带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure; /** POST请求(带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters filesData:(NSArray *)filesData resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure; @end
 
 //
// HVWBaseTool.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/10.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWBaseTool.h"
#import "HVWNetworkTool.h"
#import "MJExtension.h" @implementation HVWBaseTool /** GET请求 */
+ (void) getWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure { // 解析出参数
NSDictionary *param = [parameters keyValues]; [HVWNetworkTool get:url parameters:param success:^(id responseObject) {
if (success) {
id result = [resultClass objectWithKeyValues:responseObject];
success(result);
}
} failure:^(NSError *error) {
if (failure) {
if (failure) {
failure(error);
}
}
}];
} /** POST请求(不带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure { // 解析出参数
NSDictionary *param = [parameters keyValues]; [HVWNetworkTool post:url parameters:param success:^(id responseObject) {
if (success) {
id result = [resultClass objectWithKeyValues:responseObject];
success(result);
}
} failure:^(NSError *error) {
if (failure) {
if (failure) {
failure(error);
}
}
}];
} /** POST请求(带文件参数) */
+ (void) postWithUrl:(NSString *)url parameters:(id)parameters filesData:(NSArray *)filesData resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure { // 解析出参数
NSDictionary *param = [parameters keyValues]; [HVWNetworkTool post:url parameters:param filesData:filesData success:^(id responseObject) {
if (success) {
id result = [resultClass objectWithKeyValues:responseObject];
success(result);
}
} failure:^(NSError *error) {
if (failure) {
if (failure) {
failure(error);
}
}
}];
} @end
 
d.微博请求工具类
 //
// HVWStatusTool.h
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h>
#import "HVWBaseTool.h"
#import "HVWHomeStatusParam.h"
#import "HVWHomeStatusResult.h" @interface HVWStatusTool : HVWBaseTool /** 获取首页微博数据 */
+ (void) statusWithParameters:(HVWHomeStatusParam *)parameters success:(void (^)(HVWHomeStatusResult *statusResult))success failure:(void (^)(NSError *error))failure; @end
 
 //
// HVWStatusTool.m
// HVWWeibo
//
// Created by hellovoidworld on 15/2/9.
// Copyright (c) 2015年 hellovoidworld. All rights reserved.
// #import "HVWStatusTool.h"
#import "MJExtension.h" @implementation HVWStatusTool /** 获取首页微博数据 */
+ (void) statusWithParameters:(HVWHomeStatusParam *)parameters success:(void (^)(HVWHomeStatusResult *statusResult))success failure:(void (^)(NSError *error))failure {
// 发送请求
[self getWithUrl:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:parameters resultClass:[HVWHomeStatusResult class] success:success failure:failure];
} @end
 
==》以此类推,可以封装“发送微博”、“获取用户信息”等,其实就是一个微博API请求封装一套(参数 、结果和请求工具方法)
 
 

[iOS微博项目 - 3.5] - 封装业务的更多相关文章

  1. [iOS微博项目 - 2.5] - 封装授权和用户信息读写业务

    github: https://github.com/hellovoidworld/HVWWeibo   A.封装授权业务 1.把app的授权信息移动到HVWWeibo-Prefix.pch中作为公共 ...

  2. [iOS微博项目 - 3.3] - 封装网络请求

    github: https://github.com/hellovoidworld/HVWWeibo   A.封装网络请求 1.需求 为了避免代码冗余和对于AFN框架的多处使用导致耦合性太强,所以把网 ...

  3. [iOS微博项目 - 1.6] - 自定义TabBar

    A.自定义TabBar 1.需求 控制TabBar内的item的文本颜色(普通状态.被选中状态要和图标一致).背景(普通状态.被选中状态均为透明) 重新设置TabBar内的item位置,为下一步在Ta ...

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

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

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

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

  6. [iOS微博项目 - 2.6] - 获取微博数据

    github: https://github.com/hellovoidworld/HVWWeibo   A.新浪获取微博API 1.读取微博API     2.“statuses/home_time ...

  7. [iOS微博项目 - 1.0] - 搭建基本框架

    A.搭建基本环境   github: https://github.com/hellovoidworld/HVWWeibo   项目结构:   1.使用代码构建UI,不使用storyboard     ...

  8. [iOS微博项目 - 4.6] - 微博配图

    github: https://github.com/hellovoidworld/HVWWeibo A.微博配图 1.需求 显示原创微博.转发微博的缩略图 4张图使用2x2布局,其他使用3x3布局, ...

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

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

随机推荐

  1. JavaScript判断浏览器类型及版本(新增IE11)

    $(function () { var Sys = {}; var ua = navigator.userAgent.toLowerCase(); var s; (s = ua.match(/rv:( ...

  2. SAP R3和JAVA交换数据之JCO

    SAP Java Connector (JCo)     SAP的JAVA中间件,使用它可以使SAP的客户和合作伙伴使用JAVA语言轻松地建立可以和SAP系统通信的兼容的组件和应用程序下面是JCo一些 ...

  3. 搜索引擎根据原Sphider的脚本修正后的 Sphider-plus 2.2

    搜索引擎根据原Sphider的脚本修正后的 Sphider-plus 2.2 标签: 搜索引擎脚本search数据库authorizationjavascript -- : 1412人阅读 评论() ...

  4. Mac环境配置好ant后提示Permission denied

    1.ant环境变量配置如下 打开终端,输入vi ~/.bash_profile export ANT_HOME=/Users/administrator/Documents/software/apac ...

  5. hdu1142(dj+记忆化搜索)

    题意:给你n各点,m行关于这些点的联通关系,以及距离,求从1这个点到2这个点之间,下一个点到2这个点比当前点到2这个点的距离要小的路径的条数...... 思路:dj+记忆化搜索....... #inc ...

  6. 本系列love2d示例代码错误集中整理

    3.输入和音乐 音乐不是循环播放的,可以在love.audio.play(music) 之前添加music:setLooping(true)

  7. Unix系统编程()文件控制操作fcntl

    fcntl系统调用对一个打开的文件描述符执行一系列的控制操作. int fcntl(int fd, int cmd, -) cmd参数所支持的操作范围很广 fcntl的第三个参数以省略号表示,意味着可 ...

  8. No output operations registered, so nothing to execute

    SparkStreaming和KafKa结合报错!报错之前代码如下: object KafkaWordCount{ val updateFunc = (iter:Iterator[(String,Se ...

  9. java 错误汇总

    一.怎么处理警告:编码 GBK 的不可映射字符 解决办法是:应该使用-encoding参数指明编码方式:javac -encoding UTF-8 XX.java,这下没警告了,运行也正确了在JCre ...

  10. 基于Ambari构建自己的大数据平台产品

    目前市场上常见的企业级大数据平台型的产品主流的有两个,一个是Cloudera公司推出的CDH,一个是Hortonworks公司推出的一套HDP,其中HDP是以开源的Ambari作为一个管理监控工具,C ...