I'm having a problem sending data from Android app to php page...I have in the android side a button that takes me to this activity:
public class Post extends Activity {
public void postData(View v){
//Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.67/phptester/insertdata.php");
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(4);
try {
// Add your data
nameValuePairs.add(new BasicNameValuePair("id", "1"));
nameValuePairs.add(new BasicNameValuePair("descr", "Fire"));
nameValuePairs.add(new BasicNameValuePair("lat", "1.333"));
nameValuePairs.add(new BasicNameValuePair("lng", "1.333"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.i("postData", response.getStatusLine().toString());
} catch (IOException e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
}
And this is my php code:
<?php
$conn = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("locationdb", $conn);
if (isset($_POST["id"]) && isset($_POST['descr']) && isset($_POST['lat']) && isset($_POST['lng']))
{
$id = $_POST["id"];
$descr = $_POST["descr"];
$lat = $_POST["lat"];
$lon = $_POST["lon"];
$query = mysql_query("INSERT INTO location (ID, Description, lat, lng) VALUES('6', '$descr', '1.666', '1.666')");
echo "data inserted";
mysql_query($query);
}
else {
echo "Not data was intered ".mysql_error() ;
}
?>
The problem that is killing me is that there is no data being stored in that database and i don't know why!!!! it seems like the php can't get data from the Android app....Any help is appreciated and thanks in advance..
This isn't a solution but will help you see what data the server sends back to your app, and therefore help you debug the issue.
String response = httpclient.execute(httppost, new BasicResponseHandler());
Log.i("myapp", "Reply from server: " + response);
Now, nything you echo
back from your PHP script will be printed out in the Android log, which may make it easier to work out what's going on.
Try some troubleshooting (make sure logfile
is writable):
file_put_contents('logfile', print_r($_POST, true)."\n\n", FILE_APPEND);
Also log errors from the database:
$query = 'INSERT INTO ...';
mysql_query($query) or
file_put_contents('logfile', 'DB error: '.mysql_error()."\nQuery: ".$query."\n\n", FILE_APPEND);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.