"SBJSON.h"
"NSObject+SBJSON.h"
"NSString+SBJSON.h"
"SBJsonParser.h"
"SBJsonWriter.h"
"SBJsonBase.h"
And in all these
------
"SBJSON.h"
"NSObject+SBJSON.h"
"NSString+SBJSON.h"
------
the above 3 classes are only imported in JSON.h file
And now import JSON.h file into the class, where we are going to communicate through the HTTP JSON response into Cocoa.
Now I am going to share the way to interact with the server for GET request
1)
- (NSMutableArray *) downloadGetData:(NSString *)urlString
{
id response = [self objectWithGetUrl:[NSURL URLWithString:[NSString stringWithFormat:@"%@" ,urlString]]];
NSMutableArray *record = (NSMutableArray *)response;
return record;
}
Here we are taking the URL in string where we want to hit, to get the response, will call the method objectWithGetUrl:(NSURL*)type to get the response of the type id assigning it the value into NSURL format by converting the string into NSURL.
2)
- (id) objectWithGetUrl:(NSURL *)url
{
SBJSON *jsonParser = [SBJSON new];
NSString *jsonString = [self stringWithGetUrl:url];
// Parse the JSON into an Object
id data = [jsonParser objectWithString:jsonString error:NULL];
//Release SBJSon Object
[jsonParser release];
return data;
}
- (NSString *)stringWithGetUrl:(NSURL *)url
{
@try {
NSInteger internetConnectionAvailability = [self internetConnectionAvailable];
if(internetConnectionAvailability == kInternetConnectionAvailable)
{
NSInteger timeInterval = [NSLocalizedString(@"Time Interval", @"Time Interval") intValue];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:timeInterval];
// Fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if([urlData length] == 0)
{
UIAlertView *alertTest = [[UIAlertView alloc]
initWithTitle:@"Sorry !"
message:@"No Service is available"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[alertTest show];
[alertTest autorelease];
return @"";
}
else {
return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
}
// Construct a String around the Data from the response
}
}
@catch (NSException * e)
{
NSString *msg = [NSString stringWithFormat:@"%@",[e description]];
NSLog(@"hi NSException %@",[e description]);
UIAlertView *errorAlertView = [[[UIAlertView alloc] initWithTitle:@"Exception !" message:msg delegate:self cancelButtonTitle:@"No" otherButtonTitles:nil,nil] autorelease];
[errorAlertView show];
}
@finally
{
}
return @"";
}
@try
{
if([self internetConnectionAvailable] == kInternetConnectionAvailable)
{
SBJSON *jsonParser = [SBJSON new];
jsonParser.humanReadable = YES;
NSString *jsonString = [NSString stringWithFormat:@"%@", [refinements JSONFragment], nil];
NSString *changeJSON = [NSString stringWithFormat:@"%@", [refinements JSONFragment], nil];
#ifdef TARGET_IPHONE_SIMULATOR
NSLog(jsonString);
#endif
changeJSON = [NSString stringWithFormat:@"categoryId=%@&refinements=%@",categoryId,changeJSON];
NSData *requestData = [NSData dataWithBytes: [changeJSON UTF8String] length: [changeJSON length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:[NSString stringWithFormat: @"%@results", ServerUrl]]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];
//Data returned by WebService
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
categorySearchList = (NSMutableDictionary*) [jsonParser objectWithString:returnString error:NULL];
[[GlobalObjects sharedInstance] setCategorySearchList:categorySearchList];
[jsonParser release];
[request release];
request = nil;
[returnString release];
returnString = nil;
[[GlobalObjects sharedInstance] setCategorySearchList:categorySearchList];
[NSThread detachNewThreadSelector:@selector(saveResultForCategoryinCache) toTarget:self withObject:nil];
}
}
@catch (NSException * e)
{
NSString *msg = [NSString stringWithFormat:@"%@",[e description]];
NSLog(@"hi NSException %@",[e description]);
UIAlertView *errorAlertView = [[[UIAlertView alloc] initWithTitle:@"Exception !" message:msg delegate:self cancelButtonTitle:@"No" otherButtonTitles:nil,nil] autorelease];
[errorAlertView show];
}
@finally
{
}
No comments:
Post a Comment