Encapsulation of a network request data Class Based on AFNetWorking and afnetworking

Source: Internet
Author: User

Encapsulation of a network request data Class Based on AFNetWorking and afnetworking

1. Create a new class that inherits from the NSObject class. In the. h file

# Import "AFHTTPRequestOperationManager. h"

// Define two blocks to receive successful and failed requests

Typedef void (^ DownLoadFinishedBlock) (id responseObj );

Typedef void (^ DownLoadFialedBlock) (NSError * error );

 

@ Interface NetManager: NSObject

 

// Get Request Method Encapsulation

+ (Void) doGetWithUrlStr :( NSString *) urlString contentType :( NSString *) type finished :( DownLoadFinishedBlock) finished failure :( DownLoadFialedBlock) fialed;

// Method encapsulation of Post requests

+ (Void) doPostWithUrlStr :( NSString *) urlString parameters :( NSDictionary *) dic contentType :( NSString *) type finished :( DownLoadFinishedBlock) finished failure :( DownLoadFialedBlock) fialed;

 

 

2. The following is the implementation of the encapsulation method:

# Import "NetManager. h"

 

@ Implementation NetManager

 

// Get Request Method

+ (Void) doGetWithUrlStr :( NSString *) urlString contentType :( NSString *) type finished :( DownLoadFinishedBlock) finished failure :( DownLoadFialedBlock) fialed {

 

// Create a manager object

AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager];

// Set the request data type

Manager. responseSerializer. acceptableContentTypes = [NSSet setWithObjects: type, nil];

[Manager GET: urlString parameters: nil success: ^ (AFHTTPRequestOperation * operation, id responseObject ){

// The request is successful.

Finished (responseObject );

} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){

// Request failed

Fialed (error );

}];

}

 

// Post Request Method

// Description: parameters: parameter, which is the parameter that needs to be sent to the backend server when a post request is used. Here we should encapsulate the data in a dictionary type, then pass the dictionary as a parameter.

 

+ (Void) doPostWithUrlStr :( NSString *) urlString parameters :( NSDictionary *) dic contentType :( NSString *) type finished :( DownLoadFinishedBlock) finished failure :( DownLoadFialedBlock) fialed

{

// Create a manager object

AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager];

// Set the request data type

Manager. responseSerializer = [AFHTTPResponseSerializer serializer];

Manager. responseSerializer. acceptableContentTypes = [NSSet setWithObjects: type, nil];

[Manager POST: urlString parameters: dic success: ^ (AFHTTPRequestOperation * operation, id responseObject ){

Finished (responseObject );

} Failure: ^ (AFHTTPRequestOperation * operation, NSError * error ){

Fialed (error );

}];

}

 

 

 

 

 

@ End

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.