已解决:缺少postData()的视图参数,已更改为反映此内容。
我想帮助将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'
答案 0 :(得分:5)
堆栈跟踪的第一行告诉您确切的错误:
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'
这是因为您的View
方法声明中没有postData()
参数。
答案 1 :(得分:0)
您是否已将<uses-permission android:name="android.permission.INTERNET"></uses-permission>
添加到manifest.xml?
答案 2 :(得分:0)
我认为你应该在一个线程中调用它...如果服务器挂起一点响应,你会得到强制关闭消息......
您可以查看以下帖子:Problem with Android HTTP POST
答案 3 :(得分:0)
我认为问题在于“ nameValuePairs.add(new BasicNameValuePair(”Lat“,”19.80“)); ”您使用了 Lat 并且 $ _ POST ['lat']; ,您使用了 lat 。所以Lat和lat是不同的东西。顺便说一句,这是一篇好文章,http://androidtheme.wordpress.com/2011/02/20/connect-android-device-to-mysql/。它确实有效,我希望你喜欢它。