// The method which we are using to frame select query and parse obtained data is:
-(void)parseXMLForUser:(int)UId stringVar:(NSString*)stringVar{
@try{
if(queryByPost)
[queryByPost release];
queryByPost=[QueryByPost new]; // common class used to invoke method to send request via POST method
//obtaining data for xml parsing
NSString *query=[NSString stringWithFormat:@"Select * from userinfo update_time >= '%@' AND NOT owner_id ='%d' ",stringVar,UId];
NSData *obtainedData=[queryByPost executeQuery:query WithAction:@"query"]; // method invoked to perform post query
if(obtainedData==nil){
// data not obtained so return
return;
}
// initializing dictionary to be obtained after parsing
if(obtainedDictionary)
[obtainedDictionary release];
obtainedDictionary=[NSMutableDictionary new];
// xml parsing
if (updatedDataParser) // airportsListParser is an NSXMLParser instance variable
[updatedDataParser release];
updatedDataParser = [[NSXMLParser alloc] initWithData:obtainedData];
[updatedDataParser setDelegate:self];
[updatedDataParser setShouldResolveExternalEntities:YES];
BOOL success = [updatedDataParser parse];
}
@catch (NSException *e) {
NSLog(@"wtihin parseXMLForUser- parseXMLForUser:stringVar: - %@",[e description]);
}
}
//The method which will attempt to interact 4 times with server if interaction with it is found to be unsuccessful , is of following form:
-(NSData*)executeQuery:(NSString*)query WithAction:(NSString*)doAction{
NSLog(@"within ExecuteQuery:WithAction: Query is: %@ and Action is: %@",query,doAction);
NSString *returnResult;
@try {
NSString *returnResult;
NSMutableURLRequest *postRequest;
NSError *error;
NSData *searchData;
NSHTTPURLResponse *response;
postRequest=[self directMySQLQuery:query WithAction:doAction]; // this method sends actual POST request
NSLog(@"after directMYSQL in QueryByPost- performQuery... ErrorLogMsg");
searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
returnResult = [[NSString alloc] initWithData:searchData encoding:NSASCIIStringEncoding];
NSString *resultToBeCompared=[returnResult stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(@"result obtained - %@/ resultToBeCompared - %@",returnResult,resultToBeCompared);
if(![resultToBeCompared isEqualToString:@""]){
}else {
sleep(10);
postRequest=[self directMySQLQuery:query WithAction:doAction];
searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if(![resultToBeCompared isEqualToString:@""]){
}else {
sleep(10);
postRequest=[self directMySQLQuery:query WithAction:doAction];
searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if(![resultToBeCompared isEqualToString:@""]){
}else {
sleep(10);
postRequest=[self directMySQLQuery:query WithAction:doAction];
searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if(![resultToBeCompared isEqualToString:@""]){
}else {
sleep(10);
postRequest=[self directMySQLQuery:query WithAction:doAction];
searchData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
if(![resultToBeCompared isEqualToString:@""]){
}else {
return nil;
}
}
}
}
}
returnResult = [[NSString alloc] initWithData:searchData encoding:NSASCIIStringEncoding];
return searchData;
}
@catch (NSException * e) {
NSLog(@"within QueryByPost , execurteQuery:WithAction - %@",[e description]);
return nil;
}
}
// The method which sends POST request to server , is of following form:
-(NSMutableURLRequest *)directMySQLQuery:(NSString*)query WithAction:(NSString*)doAction{
@try{
NSLog(@"Query is: %@ and Action is: %@",query,doAction);
// some pre initialization
NSString *stringBoundary,*contentType;
NSURL *cgiUrl ;
NSMutableURLRequest *postRequest;
NSMutableData *postBody;
NSString *ans=@"434";
cgiUrl = [NSURL URLWithString:@"http://keysoftwareservices.com/API.php"];
postRequest = [NSMutableURLRequest requestWithURL:cgiUrl];
[postRequest setHTTPMethod:@"POST"];
stringBoundary = [NSString stringWithString:@"0000ABCQueryxxxxxx"];
contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", stringBoundary];
[postRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];
//setting up the body:
postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"\r\n\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"code\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:ans] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"action\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:doAction] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"devmode\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"devmode"]] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"q\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithString:query] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postRequest setHTTPBody:postBody];
NSLog(@"Direct My SQL ok");// Some time application crashes afte this log
//Some time application crashes after "Direct My SQL ok" log
return [postRequest mutableCopy];
}@catch (NSException * e) {
NSLog(@"NSException %@",e);
NSRunAlertPanel(@"Error Panel", @"Within QueryByPost- directMySQLQuery...%@", @"OK", nil, nil,e);
return nil;
}
}