Steps
first Add "ASSIHTTP" and "JSON" to your project
add all this framework to your project
in your .h file import and set delegate
#import "HttpWrapper.h" //import class then add delegete of this
@interface ViewController : UIViewController<HttpWrapperDe legate>
{
}@end
@interface ViewController : UIViewController<HttpWrapperDe
{
}@end
in your .m file call the webservice
-(void)loadWebseviceData
{
HttpWrapper *httpWrapper=[[H ttpWrapper alloc]initWithDeleg ate:self]; // create object of class with deleget method
NSMutableDictionary * dicPass=[[NSMutableDictionary alloc]init]; // create a dictionary for a send parameter
[dicPass setValue:@" Appointments" forKey:@"action" ]; //pass action to webseriver
[dicPass setValue:appDelegat e.userId forKey:@"id"]; // pass parameter
[httpWrapper requestWithMeth od:@"POST" url:WEB_URL param: dicPass selSuc:@selector( fetchDataSuccessForClient:) se lFail:@selector(fetchDataFail: )]; // fetch data
NSMutableDictionary *
[dicPass setValue:@"
[dicPass setValue:appDelegat
[httpWrapper requestWithMeth
}
// if fetch data sucess then call this
-(void) fetchDataSuccessForClient:(NSM utableDictionary *)response
{
// if fetch data fail then then call this
-(void)fetchDataFail:(NSError *)error
{
}
{
NSLog(@"%@",response);
}// if fetch data fail then then call this
-(void)fetchDataFail:(NSError *)error
{
}
HttpWrapper.h
@class ASIFormDataRequest;
@protocol HttpWrapperDelegate
@optional
- (void) fetchDataSuccess:(NSString *)response;
- (void) fetchDataFail:(NSError *)error;
- (void) fetchImageSuccess:(NSString *)response;
- (void) fetchImageFail:(NSError *)error;
@end
@interface HttpWrapper : NSObject {
NSObject<HttpWrapperDelegate> *delegate;
SEL fetchSuccess;
SEL fetchFail;
}
@property SEL fetchSuccess;
@property SEL fetchFail;
@property (nonatomic, assign) NSObject<HttpWrapperDelegate> *delegate;
-(id)initWithDelegate:(id)del;
-(void) requestWithMethod:(NSString*)method url:(NSString*)strUrl param:(NSMutableDictionary*)dictParam selSuc:(SEL)forSuc selFail:(SEL)forFail;
@end
HttpWrapper.m
#import "HttpWrapper.h"
#import "ASIFormDataRequest.h"
#import "NSObject+SBJSON.h"
#import "SBJSON.h"
@implementation HttpWrapper
@synthesize delegate, fetchSuccess, fetchFail;
-(id)initWithDelegate:(id)del {
self = [super init];
if(self) {
delegate = del;
}
return self;
}
-(void) requestWithMethod:(NSString*)method url:(NSString*)strUrl param:(NSMutableDictionary*)dictParam selSuc:(SEL)forSuc selFail:(SEL)forFail
{
fetchSuccess = forSuc;
fetchFail = forFail;
NSLog(@"HttpWrapper method:%@ >> %@ ", method, strUrl);
NSLog(@"%@",dictParam);
NSURL *url = [NSURL URLWithString:strUrl];
ASIFormDataRequest *requestMain = [ASIFormDataRequest requestWithURL:url];
[requestMain setRequestMethod:method];
if(dictParam != nil) {
NSArray *allKey = [dictParam allKeys];
for(int i=0; i<[allKey count]; i++) {
NSString *key = [allKey objectAtIndex:i];
if([key isEqualToString:@"file"]) {
NSMutableDictionary *dict = [dictParam valueForKey:key];
NSString *filepath = [dict valueForKey:@"filepath"];
NSString *filekey = [dict valueForKey:@"filekey"];
[requestMain setFile:filepath forKey:filekey];
} else {
NSString *value = [dictParam valueForKey:key];
[requestMain setPostValue:value forKey:key];
}
}
}
[requestMain setDelegate:self];
[requestMain startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request {
NSString *responseString = [request responseString];
NSLog(@"HttpWrapper > requestFinished > %@",responseString);
SBJSON *parser = [[[SBJSON alloc] init] autorelease];
NSDictionary *dic = (NSDictionary *)[parser objectWithString:responseString error:nil];
if([delegate respondsToSelector:fetchSuccess])
[delegate performSelector:fetchSuccess withObject:dic];
request = nil;
}
- (void)requestFailed:(ASIHTTPRequest *)request {
NSError *error = [request error];
NSLog(@"HttpWrapper > requestFailed > error: %@",error);
request = nil;
if([delegate respondsToSelector:fetchFail])
[delegate performSelector:fetchFail withObject:error];
}
@end

No comments:
Post a Comment