[iOS微博項目,[iOS博項目

來源:互聯網
上載者:User

[iOS微博項目,[iOS博項目
github: https://github.com/hellovoidworld/HVWWeibo 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 : NSObject12 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 HVWNetworkTool14 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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.