Simple ASIHttpRequest Encapsulation

Source: Internet
Author: User

Simple ASIHttpRequest Encapsulation
// Simple ASIHttpRequest encapsulation for iOS
// WLHTTPClient. h
// WLWLListen
//
// Created by long on 14-12-15.
// Copyright (c) 2014 WLong. All rights reserved.
//

# Import
# Import ASIFormDataRequest. h

@ Protocol WLHTTPClientDelegate
@ Optional
@ End

@ Interface WLHTTPClient: NSObject

@ Property (nonatomic, assign) id Delegate;

+ (WLHTTPClient *) sharedClient;


-(Void) postInfoWithDelegate :( id ) Delegate
WithMethod :( NSString *) method
WithParams :( NSDictionary *) params
WithSuccuessMethod :( SEL) successMethod
WithFailedMethod :( SEL) failMethod;

@ End

 

//
// WLHTTPClient. m
// WLVkoListen
//
// Created by long on 14-12-15.
// Copyright (c) 2014 WLong. All rights reserved.
//

# Import WLHTTPClient. h

# Define SuppressPerformSelectorLeakWarning (Stuff)
Do {
_ Pragma (clang diagnostic push)
_ Pragma (clang diagnostic ignored-Warc-example mselector-leaks)
Stuff;
_ Pragma (clang diagnostic pop)
} While (0)


@ Implementation WLHTTPClient

+ (WLHTTPClient *) sharedClient
{
Static WLHTTPClient * _ sharedClient = nil;
Static dispatch_once_t onceToken;
Dispatch_once (& onceToken, ^ {
_ SharedClient = [[WLHTTPClient alloc] init];

});

Return _ sharedClient;

}

-(Void) postInfoWithDelegate :( id ) Delegate
WithMethod :( NSString *) method
WithParams :( NSDictionary *) params
WithSuccuessMethod :( SEL) successMethod
WithFailedMethod :( SEL) failMethod
{

NSString * urlStr = [NSString stringWithFormat: %@%@, baseHttpUrl, method];

_ Weak ASIFormDataRequest * request = [ASIFormDataRequest requestWithURL: [NSURL URLWithString: urlStr];

[Request setRequestMethod: @ POST];

For (int I = 0; I <[params allKeys]. count; I ++ ){
[Request addPostValue: params [[params allKeys] [I] forKey: [params allKeys] [I];
}

[Request setCompletionBlock: ^ {

NSData * jsonData = request. responseData;

Id result = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: nil];

SuppressPerformSelectorLeakWarning (
[Delegate initialize mselector: successMethod withObject: result];
);

}];

[Request setFailedBlock: ^ {

SuppressPerformSelectorLeakWarning (
[Delegate initialize mselector: failMethod withObject: @ network connection failed. Please check your network connection or try again!];
);
}];

[Request startAsynchronous];


}

 

 

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.