6

Solved: Was missing view parameter for postData(), changed to reflect this.

I would like some help with sending GPS data to a server which 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 Application]

 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'
4
  • You should check your logcat output and post the stacktrace here. Commented Feb 6, 2011 at 0:06
  • @dave.c Posted the trace, from what I can see right away I have an illegalStateException, so something to do with the virtual machine not being in the correct state.... Commented Feb 6, 2011 at 0:22
  • I don't think that's your log logcat output. You can use either the view in eclipse, or the standalone tool to get the output. Have a look at this link for details. Commented Feb 6, 2011 at 0:29
  • @dave.c yeah you were right my logcat had broken when the emulator crashed, ill post the log now. Commented Feb 6, 2011 at 0:31

4 Answers 4

5

The first line of your stack trace tells you exactly 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'

That is because you do not have a View parameter in your postData() method declaration.

Sign up to request clarification or add additional context in comments.

2 Comments

@Herly: Yeah, sometimes I wish there were a direct neural interface from LogCat straight to my brain. OTOH, LogCat is rather chatty, and the other voices inside my head will get jealous.
And we would want that now would we.
0

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

3 Comments

Yeah, I am trying to send GPS coords to a database to be stored, I can access the location, I am just having a real headache sending any form of data to the server.
Yeah what... have you got that line in your manifest? Check the Logcat for a stacktrace under the Debug perspective and post it.
Sorry I wasn't clear, yes I have the line in my manifest file.
0

I think you should call it in a thread... if the server hangs a little bit to respond, you get Force close messages...

You can look at this post: Problem with Android HTTP POST

Comments

0

i think the problem is that in line "nameValuePairs.add(new BasicNameValuePair("Lat","19.80"));" you have used Lat and in line $_POST['lat'];, you have used lat. so Lat and lat are different thing. By the way, here is good article, http://androidtheme.wordpress.com/2011/02/20/connect-android-device-to-mysql/. It really works, i hope you like it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.