Objective C: Function returning correct data for the first time of call and null for other times

Posted by Kooshal Bhungy on Stack Overflow See other posts from Stack Overflow or by Kooshal Bhungy
Published on 2011-01-13T06:42:12Z Indexed on 2011/01/13 8:53 UTC
Read the original article Hit count: 198

Hi all, Am a beginner in objective C, i am implementing a function that would query a web server and display the returning string in console. I am calling the function (getDatafromServer) repeatedly in a loop. The problem is that the first time am getting the value whereas the other times, it returns me a (null) in console... I've searched about memory management and check out on the forums but none have worked. Can you please guys tell me where am wrong in the codes below? Thanks in advance....

@implementation RequestThread

+(void)startthread:(id)param{

 while (true) {
  //NSLog(@"Test threads");
  sleep(5);
  NSLog(@"%@",[self getDatafromServer]);
 }

}

+(NSString *) getDatafromServer{

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 NSString *myRequestString = @"name=Hello%20&[email protected]";

 NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:@"http://192.168.1.32/gs/includes/widget/getcalls.php?user=asdasd&passw=asdasdasd"]];
 [request setHTTPMethod:@"POST"];
 [request setHTTPBody: myRequestData];
 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
 NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

 NSString *myString = [NSString stringWithUTF8String:[returnData bytes]];

 [myRequestString release];
 [request release];
 [returnData release];

 return myString;
 [pool release];
}

@end

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about function