i am trying to insert Latitude and Longitude value in mysql data base but it show the following error
-
Error in http connection
org.apache.http.conn.HttpHostConnectException: Connection to
*http://localhost refused*//My main.java code is : public void insertdata() { ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("Lat","34")); nameValuePairs.add(new BasicNameValuePair("Lon","72")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://localhost/androidconnection/insertdata.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()); }}
//at server side my php insertion code is “insertdata.php”
<?php mysql_connect("localhost","root",""); mysql_select_db("PeopleData"); //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"; } ?>
localhostrefers to the current machine on which the code is running, which in this case is Android phone(or emulator). And in order to access the host computer from android emulator, you can use the ip address: 10.0.2.2Change your url as follows:
The line
HttpPost("http://localhost/androidconnection/insertdata.php");should look like this :
HttpPost("http://10.0.2.2/androidconnection/insertdata.php");