我在Android应用程序中调用此代码:
public static void sendAccelerationData(String userIDArg, String dateArg, String timeArg,
String timeStamp, String lat, String longi, String alt)
{
//fileName = "AddAccelerationData.php";
//Add data to be send.
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("userID", userIDArg));
nameValuePairs.add(new BasicNameValuePair("date",dateArg));
nameValuePairs.add(new BasicNameValuePair("time",timeArg));
//nameValuePairs.add(new BasicNameValuePair("timeStamp",timeStamp));
nameValuePairs.add(new BasicNameValuePair("lat",lat));
nameValuePairs.add(new BasicNameValuePair("longi",longi));
nameValuePairs.add(new BasicNameValuePair("alt",alt));
//this.sendData(nameValuePairs);
try
{
TextLog.addLogStuff("SERV Trying to connect to Server");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://myserver.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.i("ServerConn", response.getStatusLine().toString());
TextLog.addLogStuff("SERV PostData: " +response.getStatusLine().toString());
//Could do something better with response.
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
TextLog.addLogStuff("SERV Connection error: " +e.toString());
}
}
然后myserver.php的代码是:
?php
//from the online tutorial:
$usr = "bikemap";
$pwd = "pedalhard";
$db = "test";
$host = "localhost";
$cid = mysql_connect($host,$usr,$pwd);
if (!$cid) { echo("ERROR: " . mysql_error() . "\n"); }
$userID = $_POST['userID'];
$date = $_POST['date'];
$time = $_POST['time'];
$lat = $_POST['lat'];
$long = $_POST['longi'];
$alt = $_POST['alt'];
mysql_select_db("test");
mysql_query("INSERT INTO gpsdata (userID, date, time, lat, longi, alt) VALUES ('$userID', '$date', '$time', '$lat','$longi','$alt') ") or die(mysql_error());
/*$SQL = " INSERT INTO gpsdata ";
$SQL = $SQL . " (userID, date, time, lat, longi, alt) VALUES ";
$SQL = $SQL . " ('$userID', '$date', '$time', '$lat','$longi','$alt') ";
$result = mysql_query("$SQL");
if (!$result) {
echo("ERROR: " . mysql_error() . "\n$SQL\n"); } */
//echo ("New Link Added\n");
mysql_close($cid);
?>
不知何故,信息没有进入sql server。为了让方法开始,我一遍又一遍地传递6个字符串进行故障排除。这些都没有进入服务器。有什么想法吗?
编辑:
今天早上我尝试了更多故障排除。我将这些行插入到我的android代码中:
Log.i("ServerConn", response.getStatusLine().toString());
TextLog.addLogStuff("SERV PostData: " +response.getStatusLine().toString());
TextLog.addLogStuff("SERV rawData: " + nameValuePairs);
我从TextLog创建的文本文件中找到了这个:
11/11/2011 10:11:09:460 AM: SERV userID: Loren
11/11/2011 10:11:09:464 AM: SERV Trying to connect to Server
11/11/2011 10:11:11:666 AM: SERV PostData: HTTP/1.1 200 OK
11/11/2011 10:11:11:674 AM: SERV rawData: [userID=Loren, date=today, time=now,
> lat=bit 1, longi=bit 2, alt=bit 3]
所以网页或服务器一定有问题,对吧?更多信息。
编辑v2:
我将其添加到php文件中:
$string = "User ID: ";
$string = $string . $userID;
$string = $string . "Date: " . $date;
$string = $string . "\n";
$filename = 'test.txt';
$fp = fopen($filename, "a");
$write = fputs($fp, $string);
fclose($fp);
这是文本文件的内容:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
用户ID:日期:
问题1我是否使用正确的措辞写入文本文件?我今天早些时候回答了这个我正在使用正确的措辞。
问题2如果我使用的是正确的措词,那么从机器人到页面的数据会丢失吗?
谢谢大家!
答案 0 :(得分:1)
确保您已在清单文件中包含以下内容。
<uses-permission android:name="android.permission.INTERNET"></uses-permission>