[IOS Weibo project, [iOS blog Project
Github: https://github.com/hellovoidworld/HVWWeiboA. encapsulate network requests1. To avoid code redundancy and excessive coupling caused by multiple use of the AFN framework, You Need To encapsulate network requests into your own tool class, so that you can easily change the network framework in the future. 2. Create a custom tool class and provide class methods to implement network requests. 3. Implement
1 // 2 // HVWNetworkTool. h 3 // HVWWeibo O 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 Method Send request */14 + (void) get :( NSString *) url parameters :( NSDictionary *) parameters success :( void (^) (id responseObject) success failure :( void (^) (NSError * error) failure; 15 16/** post Method Send request */17 + (void) post :( NSString *) url parameters :( NSDictionary *) parameters success :( void (^) (id responseObject )) success failure :( void (^) (NSError * error) failure; 18 19/** post Method Send request (with file data) */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 O 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 Method Send request */16 + (void) get :( NSString *) url parameters :( NSDictionary *) parameters success :( void (^) (id responseObject) success failure :( void (^) (NSError * error) failure {17 // create an http operation manager 18 AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; 19 20 // send request 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 Method Send request */33 + (void) post :( NSString *) url parameters :( NSDictionary *) parameters success :( void (^) (id responseObject) success failure :( void (^) (NSError * error) failure {34 // create http operation manager 35 AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; 36 37 // send request 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 Method Send request (with file data) */51 + (void) post :( NSString *) url parameters :( NSDictionary *) parameters filesData :( NSArray *) filesData success :( void (^) (id responseObject) success failure :( void (^) (NSError * error )) failure {52 // create http operation manager 53 AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager]; 54 55 // send request 56 [manager POST: url parameters: parameters constructingBodyWithBlock: ^ (id <AFMultipartFormData> formData) {57 58 // read file parameter 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