Github:https://github.com/hellovoidworld/hvwweibo
A. Encapsulating network requests1. Requirements in order to avoid code redundancy and multiple uses of the AFN framework to cause coupling too strong, the network request is encapsulated into its own tool class, which facilitates the replacement of the network framework later. 2. Ideas create a custom tool class that provides class methods to implement network requests 3. Implementing
1 //2 //HVWNetworkTool.h3 //Hvwweibo4 //5 //Created by Hellovoidworld on 15/2/9.6 //Copyright (c) 2015 Hellovoidworld. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten One @interfaceHvwnetworktool:nsobject A - /** Get method sends request*/ -+ (void)Get:(nsstring *) URL parameters: (nsdictionary *) Parameters Success: (void(^) (IDResponseobject)) Success failure: (void(^) (Nserror *error)) Failure; the - /** Post method send request*/ -+ (void) Post: (NSString *) URL parameters: (nsdictionary *) Parameters Success: (void(^) (IDResponseobject)) Success failure: (void(^) (Nserror *error)) Failure; - + /** Post method send request (with file data)*/ -+ (void) Post: (NSString *) URL parameters: (nsdictionary *) Parameters Filesdata: (Nsarray *) Filesdata success: (void(^) (IDResponseobject)) Success failure: (void(^) (Nserror *error)) Failure; + A @end
1 //2 //HVWNETWORKTOOL.M3 //Hvwweibo4 //5 //Created by Hellovoidworld on 15/2/9.6 //Copyright (c) 2015 Hellovoidworld. All rights reserved.7 //8 9 #import "HVWNetworkTool.h"Ten #import "AFNetworking.h" One #import "HVWFileDataParam.h" A - @implementationHvwnetworktool - the /** Get method sends request*/ -+ (void)Get:(nsstring *) URL parameters: (nsdictionary *) Parameters Success: (void(^) (IDResponseobject)) Success failure: (void(^) (Nserror *error)) Failure { - //Create an HTTP operations Manager -Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager manager]; + - //Send Request +[Manager Get:url Parameters:parameters success:^ (afhttprequestoperation *operation,IDresponseobject) { A if(Success) { at success (responseobject); - } -} failure:^ (Afhttprequestoperation *operation, Nserror *error) { - if(failure) { - failure (error); - } in }]; - } to + /** Post method send request*/ -+ (void) Post: (NSString *) URL parameters: (nsdictionary *) Parameters Success: (void(^) (IDResponseobject)) Success failure: (void(^) (Nserror *error)) Failure { the //Create an HTTP operations Manager *Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager manager]; $ Panax Notoginseng //Send Request -[Manager Post:url Parameters:parameters success:^ (afhttprequestoperation *operation,IDresponseobject) { the if(Success) { + success (responseobject); A } the} failure:^ (Afhttprequestoperation *operation, Nserror *error) { + if(failure) { - failure (error); $ } $ }]; - } - the - /** Post method send request (with file data)*/Wuyi+ (void) Post: (NSString *) URL parameters: (nsdictionary *) Parameters Filesdata: (Nsarray *) Filesdata success: (void(^) (IDResponseobject)) Success failure: (void(^) (Nserror *error)) Failure { the //Create an HTTP operations Manager -Afhttprequestoperationmanager *manager =[Afhttprequestoperationmanager manager]; Wu - //Send Request About[Manager Post:url Parameters:parameters constructingbodywithblock:^ (ID<AFMultipartFormData>formData) { $ - //Read file Parameters - for(Hvwfiledataparam *filedataparaminchfilesdata) { - [FormData appendPartWithFileData:fileDataParam.fileData name:fileDataParam.name FileName:fileDataParam.fil Ename MimeType:fileDataParam.mimeType]; A } +} success:^ (Afhttprequestoperation *operation,IDresponseobject) { the if(Success) { - success (responseobject); $ } the} failure:^ (Afhttprequestoperation *operation, Nserror *error) { the if(failure) { the failure (error); the } - }]; in } the the @end
[iOS Weibo project-3.3]-Encapsulating network requests