SBJSON *json = [SBJSON new];
json.humanReadable = YES;
NSString *service = @"/getUserInfo";
//NSString *requestString = [NSString stringWithFormat:@"{"method":"%@"}", service];
NSString *requestString = [NSString stringWithFormat:@"{"firstname":"%@"}",firstName.text,nil];
NSLog(@"Request String: %@", requestString);
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSString *fileLoc = [[NSBundle mainBundle] pathForResource:@"url" ofType:@"plist" ];
NSDictionary *fileContents = [[NSDictionary alloc] initWithContentsOfFile:fileLoc];
NSString *urlLoc = [fileContents objectForKey:@"baseURL"];
urlLoc = [urlLoc stringByAppendingString:service];
NSLog(@"URL is %@",urlLoc);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:
[NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody: requestData];
//Data returned by WebService
NSError *respError = nil;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: &respError ];
if (respError) {
NSString *msg = [NSString stringWithFormat:@"Connection failed! Error - %@ %@",
[respError localizedDescription],
[[respError userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Check your network connection"
message:msg delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
NSArray *keys = [NSArray arrayWithObjects:@"firstname", @"lastname", nil];
NSArray *objects = [NSArray arrayWithObjects:@"failed to", @"refresh data...", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
allUsers = [[NSArray alloc] initWithObjects:dictionary, nil];
//[self setUserData:allUsers];
//[tblView reloadData];
//[allUsers release];
}
else
{
NSString *responseString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
NSDictionary *results = [responseString JSONValue];
// Additional steps as the webservice is adding an additional "{d:" so stripping of that
NSString *extractUsers = [results objectForKey:@"d"];
// The actual string that Web services returned, so re-scan the same and convert it as object
NSDictionary *finalResult = [extractUsers JSONValue];
allUsers = [finalResult objectForKey:@"users"];
NSLog(@"Data is : %@",allUsers);
NSLog(@"Final Value is : %@",[[allUsers objectAtIndex:0] valueForKey:@"lastname"]);
if([allUsers count]>0)
{
label.text = [[allUsers objectAtIndex:0] valueForKey:@"lastname"];
}
else
{
label.text = @"";
}
[responseString release];
[request release];
}
[inProgressIndicator stopAnimating];
[fileContents release];
//Release all the allocated data
[json release];
}