[iOS Weibo project-3.3]-Encapsulating network requests

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.