• [iOS微博项目


     
    A.封装网络请求
    1.需求
    为了避免代码冗余和对于AFN框架的多处使用导致耦合性太强,所以把网络请求封装成自己的工具类,以后便于更换网络框架。
     
    2.思路
    创建一个自定义工具类,提供类方法来实现网络请求
     
    3.实现
     1 //
     2 //  HVWNetworkTool.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 HVWNetworkTool : NSObject
    12 
    13 /** get方法发送请求 */
    14 + (void) get:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *error)) failure;
    15 
    16 /** post方法发送请求 */
    17 + (void) post:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError * error))failure;
    18 
    19 /** post方法发送请求(带文件数据) */
    20 + (void) post:(NSString *)url parameters:(NSDictionary *) parameters filesData:(NSArray *)filesData success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure;
    21 
    22 @end
     
     1 //
     2 //  HVWNetworkTool.m
     3 //  HVWWeibo
     4 //
     5 //  Created by hellovoidworld on 15/2/9.
     6 //  Copyright (c) 2015年 hellovoidworld. All rights reserved.
     7 //
     8 
     9 #import "HVWNetworkTool.h"
    10 #import "AFNetworking.h"
    11 #import "HVWFileDataParam.h"
    12 
    13 @implementation HVWNetworkTool
    14 
    15 /** get方法发送请求 */
    16 + (void) get:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError *error)) failure {
    17     // 创建http操作管理者
    18     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    19    
    20     // 发送请求
    21     [manager GET:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    22         if (success) {
    23             success(responseObject);
    24         }
    25     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    26         if (failure) {
    27             failure(error);
    28         }
    29     }];
    30 }
    31 
    32 /** post方法发送请求 */
    33 + (void) post:(NSString *)url parameters:(NSDictionary *)parameters success:(void (^)(id responseObject))success failure:(void (^)(NSError * error))failure {
    34     // 创建http操作管理者
    35     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    36    
    37     // 发送请求
    38     [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    39         if (success) {
    40             success(responseObject);
    41         }
    42     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    43         if (failure) {
    44             failure(error);
    45         }
    46     }];
    47 }
    48 
    49 
    50 /** post方法发送请求(带文件数据) */
    51 + (void) post:(NSString *)url parameters:(NSDictionary *) parameters filesData:(NSArray *)filesData success:(void (^)(id responseObject))success failure:(void (^)(NSError *error))failure {
    52     // 创建http操作管理者
    53     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    54    
    55     // 发送请求
    56     [manager POST:url parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    57        
    58         // 读取文件参数
    59         for (HVWFileDataParam *fileDataParam in filesData) {
    60             [formData appendPartWithFileData:fileDataParam.fileData name:fileDataParam.name fileName:fileDataParam.fileName mimeType:fileDataParam.mimeType];
    61         }
    62     } success:^(AFHTTPRequestOperation *operation, id responseObject) {
    63         if (success) {
    64             success(responseObject);
    65         }
    66     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    67         if (failure) {
    68             failure(error);
    69         }
    70     }];
    71 }
    72 
    73 @end
  • 相关阅读:
    RK 清理后台所有历史App任务
    RK onConfigurationChanged ConfigChanges 设备状态的改变
    RK audio 拨号同时输出Speaker和USB音频
    RK 微信视频通话预览倒立
    Unity 笔记
    C# 泛型约束为枚举
    Unity Editor 扩展PropertyDrawer (属性的 Inspector )
    Unity Editor 笔记
    Unity 反转法线,在 Hierarchy 视图对象的快捷菜单中增加 Flip Mesh Normals(反转网格法线)项
    Blender 2.9 骨骼
  • 原文地址:https://www.cnblogs.com/hellovoidworld/p/4285056.html
Copyright © 2020-2023  润新知