4つの変数をWebサーバー上のPHPファイルに投稿しようとしています。これは、私がEclipseで構築しているAndroidアプリ用です。
この関数を実行した瞬間にアプリを開くと、アプリがクラッシュします。この関数を追加する前は、アプリは正常に機能していました。アプリはエミュレーターで完璧に動作しますが、私の電話でのみクラッシュします。SamsungS3と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";
?>