Android: sending data for storage in MySQL

Solved: There is no view parameter for postData (), modified to reflect this.

I need help sending GPS data to a server that will be stored in a MySQL database using PHP.

This is inside my java file:

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()); } } 

And my 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'; ?> 

StackTrace: Project - Heat [Android app]

  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' 
+6
android php mysql
source share
4 answers

The first line of your stack trace tells you what you are doing wrong:

 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' 

This is because you do not have a View parameter in the declaration of the postData() method.

+5
source share

Have you added <uses-permission android:name="android.permission.INTERNET"></uses-permission> to manifest.xml?

0
source share

I think you should name it in the stream ... if the server freezes a bit to respond, you get Force close messages ...

You can see the message: Problem with HTTP HTTP POST

0
source share

I think the problem is that in the line " nameValuePairs.add (new BasicNameValuePair (" Lat "," 19.80 ")); " you used Lat in line $ _ POST ['lat']; , you used lat . therefore Lat and lat are two different things. By the way, here is a good article, http://androidtheme.wordpress.com/2011/02/20/connect-android-device-to-mysql/ . It really works, I hope you enjoy it.

0
source share

All Articles