2011-11-17 46 views
0

我真的需要一些幫助,我的項目。發送數據從android到mysql

我有一個android應用程序,掃描無線網絡。我想將這些數據上傳到mysql數據庫。

的Android有數據庫類:

public class Database { 

public static void putServerData(String building, String floor, String room, String address, String signal){ 
    String db_url = "http://**(IP ADDRESS)**/setdata.php"; 

    @SuppressWarnings("unused") 
    InputStream is = null; 
    ArrayList<NameValuePair> request = new ArrayList<NameValuePair>(); 
    request.add(new BasicNameValuePair("building",building)); 
    request.add(new BasicNameValuePair("floor",floor)); 
    request.add(new BasicNameValuePair("room",room)); 
    request.add(new BasicNameValuePair("address",address)); 
    request.add(new BasicNameValuePair("signal",signal)); 


    try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(db_url); 
     httppost.setEntity(new UrlEncodedFormEntity(request)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 

    }catch(Exception e){ 

    } 

} 

這就是所謂的:

  try { 
       Database.putServerData(building,floor, room, array1.get(1).toString(), array2.get(2).toString());     
      } catch (Exception e) { 
       Log.e("test", "test"); 
      } 

該服務器是Win7的機器,與Apache PHP和MySQL

的setdata.php文件:

<?php 
    $con = mysql_connect("localhost","root","pass"); 
    if(!$con) 
    { 
echo 'Not connected'; 
echo ' - '; 

    }else 
    { 
echo 'Connection Established'; 
echo ' - '; 
} 

$db = mysql_select_db("android"); 
if(!$db) 
{ 
echo 'No database selected'; 
}else 
{ 
echo 'Database selected'; 
} 



$building = $_POST['building']; 
$floor = $_POST['floor']; 
$room = $_POST['room']; 
$ap_mac1 = $_POST['ap_mac1']; 
$ap_strength1 = $_POST['ap_strength1']; 
$ap_mac2 = $_POST['ap_mac2']; 
$ap_strength2 = $_POST['ap_strength2']; 
$ap_mac3 = $_POST['ap_mac3']; 
$ap_strength3 = $_POST['ap_strength3']; 
$ap_mac4 = $_POST['ap_mac4']; 
$ap_strength4 = $_POST['ap_strength4']; 
$ap_mac5 = $_POST['ap_mac5']; 
$ap_strength5 = $_POST['ap_strength5']; 

    echo ($_POST['building']); 

mysql_query("INSERT INTO wifiscan VALUES($nextid, '$building','$floor','$room','$ap_mac1','$ap_strength1','$ap_mac2','$ap_strength2', '$ap_mac3', '$ap_strength3', '$ap_mac4', '$ap_strength4', '$ap_mac5', '$ap_strength5')"); 



mysql_close(); ?> 

這不起作用。林不知道我以正確的方式選擇數據庫,這可能是問題嗎?

也許這不是很清楚,生病會進一步解釋,如果你想。

+1

你是什麼意思不工作?發生什麼事? –

+0

我試圖在php文件中設置一些輸出(請參閱編輯的php代碼) 在瀏覽器中的輸出:連接建立 - 數據庫選擇 但代碼回聲($ _POST ['building']);不要輸出。 – freddy

+0

你必須給我們更多的細節。就像放入日誌行一樣,並檢查代碼的確切位置。也許我們可以弄明白。 –

回答

1

使用一些日誌,也許sprintfs找出究竟發生了什麼。我開始使用PHP。嘗試從瀏覽器打開URL並在PHP中輸入一些輸出以查看是否一切正常。發佈你的結果,你可能會得到進一步的幫助。

+0

我試過了,看到新的PHP代碼。 – freddy

+0

您是如何在測試中提交數據的?你有沒有發過來的東西?表單是否設置爲「POST」?你有沒有試過GET? – HOK

+0

我試過POST和GET。 我錯過了什麼嗎? 再次感謝。 – freddy