已解決:缺少postData()的視圖參數,更改爲反映此情況。Android:發送要存儲在MySQL中的數據
我想要一些幫助,將GPS數據發送到服務器,該服務器將使用PHP存儲在MySQL數據庫中。
這是我的Java文件中:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
public void postData(View v)
{
nameValuePairs.add(new BasicNameValuePair("Lat","19.80"));
nameValuePairs.add(new BasicNameValuePair("Lon","13.22"));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://www.xxxxxxxx.com/test.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
我的PHP:
<?php
include 'connect.php'; //Connect
//retrieve the data
$lat = $_POST['lat'];
$lon = $_POST['lon'];
$sql = "INSERT INTO Coords (Lat, Lon) VALUES('$lat', '$lon')";
if (!mysql_query($sql, $sqlCon))
{
die('Error: ' . mysql_error());
}
else
{
//echo "1 record added";
}
include 'closeConnection.php';
?>
堆棧跟蹤: 項目 - 熱火[Android應用程序]
02-06 00:37:14.265: ERROR/AndroidRuntime(1607): FATAL EXCEPTION: main
02-06 00:37:14.265: ERROR/AndroidRuntime(1607): java.lang.IllegalStateException: Could
not find a method **postData(View)** in the activity class com.nathanhunston.heat.Main for
onClick handler on view class android.widget.Button with id 'dropLocBtn'
你應該檢查你的logcat輸出並在這裏發佈stacktrace。 – 2011-02-06 00:06:57
@ dave.c發佈了跟蹤,從我可以看到馬上我有一個illegalStateException,所以與虛擬機不處於正確的狀態.... – nhunston 2011-02-06 00:22:21
我不認爲這是你的日誌logcat輸出。您可以使用eclipse中的視圖,也可以使用獨立工具來獲取輸出。有關詳細信息,請參閱[此鏈接](http://developer.android.com/guide/developing/debug-tasks.html)。 – 2011-02-06 00:29:08