我从 iOs 设备发送一个 jsonString
看起来像这样:
2012-10-10 08:50:32.011 Appname[4049:c07] Post String =http://www.yourdomain.nl/locatie.php?data=%7B%22id%22:%220612833397%22,%22longitude%22:%22-143.406417%22,%22latitude%22:%2232.785834%22,%22timestamp%22:%2210-10%2007:56%22%7D
那是我 NSLog 它的时候...
所以 PHP 文件看起来像这样:
<?php
$id = $_POST['id'];
$longitude = $_POST['longitude'];
$latitude = $_POST['latitude'];
$timestamp = $_POST['stringFromDate'];
$link = mysql_connect('server', 'sbla', 'bla')
or die('Could not connect: ' . mysql_error());
mysql_select_db('md267052db227433') or die('Could not select database');
// Performing SQL query
$query = "INSERT INTO locatie (id, longitude, latitude, timestamp) VALUES (NULL," . $longitude . "," . $latitude . "," .$timestamp." )";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo "OK";
// Free resultset
mysql_free_result($result);
// Closing connection
mysql_close($link);
?>
当我转到 yourdomain.nl/locatie.php 时,它说它是空的,但这应该是因为我从我的 iOs 应用程序发送数据。
我希望我可以看到 id、经度、纬度和时间戳的 jSonstring 并将它们放入 mySQL 数据库中,但奇怪的是它不起作用。
我知道来自 iOs 设备的字符串已发送。
对不起,我的英语不好,
任何帮助,将不胜感激。
编辑
像这样发送:
- (void)myFuntionThatWritesToDatabaseInBackgroundWithLatitude:(NSString *)latitude longitude:(NSString *)longitude date:
(NSString *)stringFromDate {
_phonenumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"phoneNumber"];
NSMutableString *postString = [NSMutableString stringWithString:kPostURL];
NSString*jsonString = [[NSString alloc] initWithFormat:@"{\"id\":\"%@\",\"longitude\":\"%@\",\"latitude\":\"%@\",\"timestamp\":\"%@\"}",_phonenumber, longitude , latitude, stringFromDate];
[postString appendString:[NSString stringWithFormat:@"?data=%@", jsonString]];
[postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[[NSURLConnection alloc] initWithRequest:request delegate:self ];
NSLog(@"Post String =%@", postString);
// LocationTestViewController*locationTestViewController = [[LocationTestViewController alloc]init];
// phonenumber = locationTestViewController.telefoonnummer;
NSLog(@"telnr : %@", _phonenumber);
NSURLResponse* response;
NSHTTPURLResponse* httpResponse;
NSError* error;
NSData* responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* stringResponse = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
httpResponse = (NSHTTPURLResponse*) response;
int statuscode = [httpResponse statusCode];
if (statuscode == 200)
{
NSLog(@"Verstuurd");
// Handle the response here if needed
}
else
{
NSLog(@"niet verstuurd: %@", stringResponse);
// Show some form of alert here if needed
}
// release all objects saved to memory
[request release];
request = nil;
[stringResponse release];
stringResponse = nil;
}