Đã giải quyết: Thiếu thông số chế độ xem cho postData(), được thay đổi để phản ánh điều này.Android: Gửi dữ liệu được lưu trữ trong MySQL
Tôi muốn được trợ giúp gửi dữ liệu GPS đến máy chủ sẽ được lưu trữ trong cơ sở dữ liệu MySQL bằng PHP.
Đây là bên trong tập tin Java của tôi:
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());
}
}
Và PHP của tôi:
<?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';
?>
StackTrace: Dự án - Heat [Android Ứng dụng]
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'
Bạn nên kiểm tra đầu ra logcat của mình và đăng stacktrace tại đây. –
@ dave.c Đăng theo dõi, từ những gì tôi có thể thấy ngay lập tức tôi có một exceptionStateException, do đó, một cái gì đó để làm với máy ảo không được ở trạng thái chính xác .... – nhunston
Tôi không nghĩ đó là đầu ra logcat log của bạn . Bạn có thể sử dụng chế độ xem trong nhật thực hoặc công cụ độc lập để nhận kết quả. Hãy xem [liên kết này] (http://developer.android.com/guide/developing/debug-tasks.html) để biết chi tiết. –