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 );
}
}