zoukankan      html  css  js  c++  java
  • [iOS微博项目

     
    A.封装微博业务
    1.需求
    把微博相关业务(读取、写微博)
    界面控制器不需要知道微博操作细节(例如选择从网络读取还是缓存读取)
     
    2.实现
    把微博操作封装成一个工具类
    把微博网络请求的参数和返回结果也封装成一个类
     
    Image(129)
     
    Image(130)
     
    3.实现
    (1)基础参数类
    由于多数请求都需要access_token,所以封装一个参数父类
     1 //
     2 //  HVWBaseParam.h
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface HVWBaseParam : NSObject
    12 
    13 /** 访问令牌 */
    14 @property(nonatomic, copy) NSString *access_token;
    15 
    16 @end
     
     1 //
     2 //  HVWBaseParam.m
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "HVWBaseParam.h"
    10 #import "HVWAccountInfo.h"
    11 #import "HVWAccountInfoTool.h"
    12 
    13 @implementation HVWBaseParam
    14 
    15 - (NSString *)access_token {
    16     if (nil == _access_token) {
    17         _access_token = [HVWAccountInfoTool accountInfo].access_token;
    18     }
    19     return _access_token;
    20 }
    21 
    22 @end
     
    (2)首页获取微博
    a.参数类
    根据微博API请求参数列表
    Image(131)
     
     1 //
     2 //  HVWHomeStatusParam.h
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "HVWBaseParam.h"
    10 
    11 @interface HVWHomeStatusParam : HVWBaseParam
    12 
    13 /** false string 采用OAuth授权方式不需要此参数,其他授权方式为必填参数,数值为应用的AppKey。 */
    14 @property(nonatomic, copy) NSString *source;
    15 
    16 /** false int64 若指定此参数,则返回ID比since_id大的微博(即比since_id时间晚的微博),默认为0。 */
    17 @property(nonatomic, strong) NSNumber *since_id;
    18 
    19 /** false   int64 若指定此参数,则返回ID小于或等于max_id的微博,默认为0。 */
    20 @property(nonatomic, strong) NSNumber *max_id;
    21 
    22 /** false int 单页返回的记录条数,最大不超过100,默认为20。 */
    23 @property(nonatomic, strong) NSNumber *count;
    24 
    25 /** false int 返回结果的页码,默认为1。 */
    26 @property(nonatomic, strong) NSNumber *page;
    27 
    28 /** false int 是否只获取当前应用的数据。0为否(所有数据),1为是(仅当前应用),默认为0。 */
    29 @property(nonatomic, strong) NSNumber *base_app;
    30 
    31 /** false int 过滤类型ID,0:全部、1:原创、2:图片、3:视频、4:音乐,默认为0。 */
    32 @property(nonatomic, strong) NSNumber *feature;
    33 
    34 /** false int 返回值中user字段开关,0:返回完整user字段、1:user字段仅返回user_id,默认为0。 */
    35 @property(nonatomic, strong) NSNumber *trim_user;
    36 
    37 @end
     
    b.返回结果类
    微博返回结果是N条微博数据的数组
    (这里在返回结果包装类中直接使用之前创建的HVWStatus类来封装微博数据)
    Image(132)
     
    每个status里面的元素:
    Image(133)
     
     1 //
     2 //  HVWHomeStatusResult.h
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "HVWStatus.h"
    11 
    12 @interface HVWHomeStatusResult : NSObject
    13 
    14 /** 微博数组,里面装的HVWStatus模型 */
    15 @property(nonatomic, strong) NSArray *statuses;
    16 
    17 /** 近期微博总数 */
    18 @property(nonatomic, assign) int total_number;
    19 
    20 @end
     
     1 //
     2 //  HVWHomeStatusResult.m
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "HVWHomeStatusResult.h"
    10 #import "MJExtension.h"
    11 
    12 @implementation HVWHomeStatusResult
    13 
    14 /** 指明将json数组元素转为什么模型类 */
    15 - (NSDictionary *)objectClassInArray {
    16     return @{@"statuses":[HVWStatus class]};
    17 }
    18 
    19 @end
     
    c.公共请求工具类
    内部处理模型转字典的逻辑
     1 //
     2 //  HVWBaseTool.h
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/10.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 
    11 @interface HVWBaseTool : NSObject
    12 
    13 /** GET请求 */
    14 + (void) getWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure;
    15 
    16 /** POST请求(不带文件参数) */
    17 + (void) postWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure;
    18 
    19 /** POST请求(带文件参数) */
    20 + (void) postWithUrl:(NSString *)url parameters:(id)parameters filesData:(NSArray *)filesData resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure;
    21 
    22 @end
     
     1 //
     2 //  HVWBaseTool.m
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/10.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "HVWBaseTool.h"
    10 #import "HVWNetworkTool.h"
    11 #import "MJExtension.h"
    12 
    13 @implementation HVWBaseTool
    14 
    15 /** GET请求 */
    16 + (void) getWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    17    
    18     // 解析出参数
    19     NSDictionary *param = [parameters keyValues];
    20    
    21     [HVWNetworkTool get:url parameters:param success:^(id responseObject) {
    22         if (success) {
    23             id result = [resultClass objectWithKeyValues:responseObject];
    24             success(result);
    25         }
    26     } failure:^(NSError *error) {
    27         if (failure) {
    28             if (failure) {
    29                 failure(error);
    30             }
    31         }
    32     }];
    33 }
    34 
    35 /** POST请求(不带文件参数) */
    36 + (void) postWithUrl:(NSString *)url parameters:(id)parameters resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    37    
    38     // 解析出参数
    39     NSDictionary *param = [parameters keyValues];
    40    
    41     [HVWNetworkTool post:url parameters:param success:^(id responseObject) {
    42         if (success) {
    43             id result = [resultClass objectWithKeyValues:responseObject];
    44             success(result);
    45         }
    46     } failure:^(NSError *error) {
    47         if (failure) {
    48             if (failure) {
    49                 failure(error);
    50             }
    51         }
    52     }];
    53 }
    54 
    55 /** POST请求(带文件参数) */
    56 + (void) postWithUrl:(NSString *)url parameters:(id)parameters filesData:(NSArray *)filesData resultClass:(Class)resultClass success:(void (^)(id))success failure:(void (^)(NSError *))failure {
    57    
    58     // 解析出参数
    59     NSDictionary *param = [parameters keyValues];
    60    
    61     [HVWNetworkTool post:url parameters:param filesData:filesData success:^(id responseObject) {
    62         if (success) {
    63             id result = [resultClass objectWithKeyValues:responseObject];
    64             success(result);
    65         }
    66     } failure:^(NSError *error) {
    67         if (failure) {
    68             if (failure) {
    69                 failure(error);
    70             }
    71         }
    72     }];
    73 }
    74 
    75 @end
     
    d.微博请求工具类
     1 //
     2 //  HVWStatusTool.h
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import <Foundation/Foundation.h>
    10 #import "HVWBaseTool.h"
    11 #import "HVWHomeStatusParam.h"
    12 #import "HVWHomeStatusResult.h"
    13 
    14 @interface HVWStatusTool : HVWBaseTool
    15 
    16 /** 获取首页微博数据 */
    17 + (void) statusWithParameters:(HVWHomeStatusParam *)parameters success:(void (^)(HVWHomeStatusResult *statusResult))success failure:(void (^)(NSError *error))failure;
    18 
    19 @end
     
     1 //
     2 //  HVWStatusTool.m
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "HVWStatusTool.h"
    10 #import "MJExtension.h"
    11 
    12 @implementation HVWStatusTool
    13 
    14 /** 获取首页微博数据 */
    15 + (void) statusWithParameters:(HVWHomeStatusParam *)parameters success:(void (^)(HVWHomeStatusResult *statusResult))success failure:(void (^)(NSError *error))failure {
    16     // 发送请求
    17     [self getWithUrl:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:parameters resultClass:[HVWHomeStatusResult class] success:success failure:failure];
    18 }
    19 
    20 @end
     
    ==》以此类推,可以封装“发送微博”、“获取用户信息”等,其实就是一个微博API请求封装一套(参数 、结果和请求工具方法)
     
     
  • 相关阅读:
    Python基础之逻辑运算符
    Python基础之赋值运算符
    Python基础之算术运算符
    Python基础之格式化输出
    Python基础之while循环
    Python基础之if语句
    Python基础之注释
    Python基础之变量和常量
    Python基础之Python解释器
    Flask-登录练习
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4285062.html
Copyright © 2011-2022 走看看