我試圖插入MySQL數據庫經緯度值,但它顯示了以下錯誤的HTTP連接 org.apache.http連接到MySQL數據庫錯誤
錯誤。 conn.HttpHostConnectException:連接到 * 的http://本地主機拒絕 *
//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"; } ?>
http://stackoverflow.com/questions/5114301/android-sdk-connect-to-web-server-on-host – 2012-12-15 08:27:12