我想让我的android应用程序发送ping请求,并将往返时间存储在脱机数据库中。 ping部分有效,但是在数据库中存储数据不起作用。
作为一个例子,我使用了静态参数:
public void upload(Context context)
{
String rtt = "4.4";
String location = "4.4";
String TAG = "MyActivity";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://somesite.com);
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("6.2", rtt));
postParameters.add(new BasicNameValuePair("6.3", location));
httppost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpclient.execute(httppost);
Log.i("postData", response.getStatusLine().toString());
}
我希望这会在以下php脚本中上传这些参数。 PHP又将这些值存储在mysql数据库中(我希望)
<?php
$con = mysqli_connect("localhost","****","******","DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
//you need to exit the script, if there is an error
exit();
}
mysqli_select_db($con,"stats");
$rtt = $_POST['rtt'];
$location = $_POST['location'];
mysqli_query("INSERT INTO stats (rtt, location) VALUES ('$rtt','$location')");
mysqli_close($con);
?>
不可否认这不起作用。我不知道在哪里查找日志,apache错误和访问日志除了以外不提供太多信息:
[2014年6月18日:22:15:31 +0200]&#34; POST /sql.php HTTP / 1.1&#34; 500 231&#34; - &#34; &#34; Apache-HttpClient / UNAVAILABLE(java 1.4)&#34;
至少消息正在进入。我应该在哪里寻找更多信息?我不确定是否将正确的信息发送到php网站。
我已使用以下php文件进行测试:
$con = mysqli_connect("localhost","USER","PASS", "DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
//you need to exit the script, if there is an error
exit();
}
mysqli_select_db($con,"stats");
$rtt = $_POST['rtt'];
$location = $_POST['location'];
mysqli_query($con, "INSERT INTO stats (rtt, location) VALUES ('0.5','2.5')");
mysqli_close($con);
?>
此信息将添加到数据库中。