我正在尝试制作一个简单的应用程序,将数据发布到我家的MySQL服务器(WAMP或MAMP),这样当我们去圣路易斯参加机器人比赛时,我们就可以将有关团队的球探数据添加到数据库中,稍后再从中提取。
我很难将数据发布到我设置的表中。
这是目标C代码:
NSString *myRequestString = [NSString stringWithFormat:@"teamNumber=%@&matchNumber=%@&autoGoal=%@&autoReachedOW=%@&autoCrossedDef=%@&teleopOffense=%@&teleopCrossedDef=%@&teleopHighGoalsMissed=%@&teleopHighGoalsMade=%@&teleopLowGoalsMissed=%@&teleopLowGoalsMade=%@&teleopEndGame=%@&teleopDefense=%@&teleopShotsDef=%@&teleopShotsDisrupted=%@&teleopPenalties=%@",
teamNumber,
matchNumber,
autoGoal,
autoReachedOW,
autoCrossedDef,
teleopOffense,
teleopCrossedDef,
teleopHighGoalsMissed,
teleopHighGoalsMade,
teleopLowGoalsMissed,
teleopLowGoalsMade,
teleopEndGame,
teleopDefense,
teleopShotsDef,
teleopShotsDisrupted,
teleopPenalties];
NSData *myRequestData = [NSData dataWithBytes: [myRequestString UTF8String] length: [myRequestString length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: @"http://localhost:8888/add.php"]];
// set Request Type
[request setHTTPMethod: @"POST"];
// Set content-type
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
// Set Request Body
[request setHTTPBody: myRequestData];
// Now send a request and get Response
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil];
// Log Response
NSString *response = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSLog(@"%@",response);
以下是我从iPhone应用程序发出请求时使用的PHP代码:
<?php
$servername = “localhost:8888”;
$username = “root”;
$password = “root”;
$dbname = “teamdata”;
$db_conn = new mysqli($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
echo "Connection Failed.";
}
$teamNumber = ($_POST['teamNumber']);
$matchNumber = ($_POST['matchNumber']);
$autoGoal = ($_POST['autoGoal']);
$autoReachedOW = ($_POST['autoReachedOW']);
$autoCrossedDef = ($_POST['autoCrossedDef']);
$teleopOffense = ($_POST['teleopOffense']);
$teleopCrossedDef = ($_POST['teleopCrossedDef']);
$teleopHighGoalsMissed = ($_POST['teleopHighGoalsMissed']);
$teleopHighGoalsMade = ($_POST['teleopHighGoalsMade']);
$teleopLowGoalsMissed = ($_POST['teleopLowGoalsMissed']);
$teleopLowGoalsMade = ($_POST['teleopLowGoalsMade']);
$teleopEndGame = ($_POST['teleopEndGame']);
$teleopDefense = ($_POST['teleopDefense']);
$teleopShotsDef = ($_POST['teleopShotsDef']);
$teleopShotsDisrupted = ($_POST['teleopShotsDisrupted']);
$teleopPenalties = ($_POST['teleopPenalties']);
$qry = 'INSERT INTO appdata (`teamNumber`,`matchNumber`,`autoGoal`,`autoReachedOW`,`autoCrossedDef`,`teleopOffense`,`teleopCrossedDef`, `teleopHighGoalsMissed`, `teleopHighGoalsMade`, `teleopLowGoalsMissed`, `teleopLowGoalsMade`, `teleopEndGame`, `teleopDefense`, `teleopShotsDef`, `teleopShotsDisrupted`, `teleopPenalties`) VALUES ($teamNumber,$matchNumber,$autoGoal,$autoReachedOW,$autoCrossedDef,$teleopOffense,$teleopCrossedDef,$teleopHighGoalsMissed,$teleopHighGoalsMade,$teleopLowGoalsMissed,$teleopLowGoalsMade,$teleopEndGame,$teleopDefense,$teleopShotsDef,$teleopShotsDisrupted,$teleopPenalties)';
if ($qry) { $message = "success"; }
else { $message = "failed"; }
echo utf8_encode($message);
?>
当我运行应用程序并按下保存按钮(运行Objective C代码)时,我不会在本地服务器上的表中添加任何行。
我没有得到任何NSLog输出。
如果有人能为我指明正确的方向,请这样做。如果你需要更多信息,请告诉我。我将对此进行几个小时的监控。非常感谢。
您可以尝试以下几种方法:
首先,检查web服务器上的日志。这将为您提供正在处理的任何请求的状态代码。
其次,当运行sendSynchronousRequest
时,应该设置returningResponse
和error
变量的值。这些将为您提供有关刚刚运行的请求的大量信息。
当您同步运行请求时(顺便说一句,这不是最佳实践),您将能够在该请求之后的代码行上设置一个断点,并检查这两个变量的内容。
默认情况下,sendSynchronousRequest
不会向控制台吐出任何内容,因此必须手动执行。