我正在尝试将 4 个变量发布到网络服务器上的 PHP 文件中。这是针对我在 Eclipse 中构建的 android 应用程序。
当我打开应用程序运行此功能的那一刻,应用程序崩溃。在我添加此功能之前,该应用程序运行良好。该应用程序在模拟器中完美运行,但仅在我的手机上崩溃。我尝试在三星 S3 和 S2 上运行它。
Android 应用程序中的功能。使用 log.di 能够发现应用程序运行到 try 语句而不是崩溃。
public void postData( double pLong, double pLat, String timeStamp) throws ClientProtocolException, IOException, URISyntaxException {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://website.com/this.php");
Log.d("response", "WORKING");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("IMEI", deviceid));
nameValuePairs.add(new BasicNameValuePair("LONGITUDE",Double.toString(pLat)));
nameValuePairs.add(new BasicNameValuePair("LATITUDE", Double.toString(pLong)));
nameValuePairs.add(new BasicNameValuePair("CREATED_AT", timeStamp));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
Log.d("response!!!!", " " + response);
} catch (ClientProtocolException e) {
//Requirement Auto-generated catch block
Log.d("response", "nope" + e);
} catch (IOException e) {
Log.d("response", "nope" + e);
//Requirement Auto-generated catch block
}
}
PHP 文件:
<?php
$imei = $_POST['IMEI'];
$long = $_POST['LONGITUDE'];
$lat = $_POST['LATITUDE'];
$time = $_POST['CREATED_AT'];
$con = mysql_connect("host", "username", "password"); //this is the real username and password
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
mysql_query("INSERT INTO `table`(`imei`, `longitude`, `latitude`, `createdAt`)
VALUES ('".$imei."', '".$long."', '".$lat."', '".$time."')") or die (mysql_error());
$id = mysql_insert_id();
echo "done";
?>