我正在尝试在 mysql 数据库中插入纬度和经度值,但它显示以下错误
http 连接 org.apache.http.conn.HttpHostConnectException 出错:连接到 * http://localhost被拒绝*
//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()); }
}
//在服务器端我的php插入代码是“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"; } ?>