Hi,
I am writing a sample PHP Web Service that is sending GET Http request from the iphone to the web server. The server side code is returning JSON Data to iphone, the server side code looks like:
function getUsers(){
$con=mysql_connect("localhost","root","123456") or die(mysql_error());
if(!mysql_select_db("eventsfast",$con))
{
echo "Unable to connect to DB";
exit;
}
$sql="SELECT * from users";
$result=mysql_query($sql);
if(!$result)
{
die('Could not successfully run query Error:'.mysql_error());
}
$data = array();
while($row = mysql_fetch_assoc($result)){
$data['username'][] = $row['username'];
$data['password'][]= $row['password'];
}
mysql_close($con);
//print_r(json_encode($data));
//return (json_encode($data));
return json_encode('success');
}
getUsers();
?
Its a simple code that Fetches all the data from the user table and send it to the iphone application.
*****************************************************IPHONE APPLICATION
(void)viewDidLoad {
[super viewDidLoad];
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8888/GetData.php"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
}
(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSError *error;
SBJSON *json = [[SBJSON new] autorelease];
NSArray *luckyNumbers = [json objectWithString:responseString error:&error];
[responseString release];
if (luckyNumbers == nil)
label.text = [NSString stringWithFormat:@"JSON parsing failed: %@", [error localizedDescription]];
else {
NSMutableString *text = [NSMutableString stringWithString:@"Lucky numbers:\n"];
for (int i = 0; i < [luckyNumbers count]; i++)
[text appendFormat:@"%@\n", [luckyNumbers objectAtIndex:i]];
NSLog(text);
label.text = text;
}
}
********************PROBLEM****************
The problem is that nothing is coming on iphone side of the application........... the response doesn't contains anything..............
Please help..............