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