AFNetworking, afnetworking

Source: Internet
Author: User

AFNetworking, afnetworking

// Get request data

+ (Void) get :( NSString *) urlStr params :( NSDictionary *) params success :( void (^) (id) success failure :( void (^) (NSError *) failure

{

// 1. Obtain the request Manager

AFHTTPRequestOperationManager * mgr = [AFHTTPRequestOperationManager];

NSString * URL = [NSString stringWithFormat: @ "% @", HOST, urlStr];

NSLog (@ "% @", URL );

// 2. Send a GET request

[Mgr GET: URL parameters: params

Success: ^ (AFHTTPRequestOperation * operation, id responseObj ){

If (success ){

Success (responseObj );

}

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

If (failure ){

Failure (error );

}

}];

}

 

// Post request data

+ (Void) post :( NSString *) urlStr params :( NSDictionary *) params success :( void (^) (id) success failure :( void (^) (NSError *) failure

{

If ([[self class] checkNetworkState]) {

// Obtain public Parameters

NSMutableDictionary * requestParms = [[self class] publicParms];

If (params! = Nil ){

// Not empty merge dictionary, public parameter dictionary

[RequestParms addEntriesFromDictionary: params];

}

// 1. Obtain the request Manager

AFHTTPRequestOperationManager * mgr = [AFHTTPRequestOperationManager];

/******** This is very important ********/

// 1. mgr. responseSerializer = [AFJSONResponseSerializer serializer];

// 2. mgr. requestSerializer = [AFJSONRequestSerializer serializer];

// 3. mgr. responseSerializer. acceptableContentTypes = [NSSet setWithObject: @ "application/json"];

 

NSString * URL = [NSString stringWithFormat: @ "% @", HOST, urlStr];

NSLog (@ "current interface URL =%@", URL );

NSLog (@ "parameters of the current request ==%@", requestParms );

// 2. Send a POST request

[Mgr POST: URL parameters: requestParms success: ^ (AFHTTPRequestOperation * operation, id responseObj ){

Success (responseObj );

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

Failure (error );

[MBProgressHUD hideAllHUDsForView: [UIApplication sharedApplication]. keyWindow animated: YES];

NSLog (@ "underlying failure = error = % @", [error localizedDescription]);

[MBProgressHUD showError: @ "network is not powerful. Try again later" toView: [UIApplication sharedApplication]. keyWindow];

}];

} Else {

[MBProgressHUD hideAllHUDsForView: [UIApplication sharedApplication]. keyWindow animated: YES];

[MBProgressHUD showError: @ "the network is disconnected. Check the network connection" toView: [UIApplication sharedApplication]. keyWindow];

Failure (nil );

}

}

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.