2011-08-04 69 views
0

我是新來的android開發和 我想開發一個android的應用程序,將數據發送到我的網站(例如經度&緯度),我的問題是如何從android發送數據以及如何在網站上獲取/捕獲它。從android發送數據到網站和在網上接收

+0

你需要學習XML解析和http響應 –

回答

1

這可能有幫助。僅僅是我之前發送加速數據到服務器的例子,所以我可以將數據存儲到mysql數據庫中。 爲了將數據發送到服務器,你可以這樣做:

private void sendData(ArrayList<NameValuePair> data) 
{ 
    // 1) Connect via HTTP. 2) Encode data. 3) Send data. 
    try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new  
     HttpPost("http://www.blah.com/AddAccelerationData.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(data)); 
     HttpResponse response = httpclient.execute(httppost); 
     Log.i("postData", response.getStatusLine().toString()); 
      //Could do something better with response. 
    } 
    catch(Exception e) 
    { 
     Log.e("log_tag", "Error: "+e.toString()); 
    } 
} 

然後派可以說:

private void sendAccelerationData(String userIDArg, String dateArg, String timeArg, 
     String timeStamp, String accelX, String accelY, String accelZ) 
{ 
    fileName = "AddAccelerationData.php"; 

    //Add data to be send. 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(7); 
    nameValuePairs.add(new BasicNameValuePair("userID", userIDArg)); 
    nameValuePairs.add(new BasicNameValuePair("date",dateArg)); 
    nameValuePairs.add(new BasicNameValuePair("time",timeArg)); 
    nameValuePairs.add(new BasicNameValuePair("timeStamp",timeStamp)); 

    nameValuePairs.add(new BasicNameValuePair("accelX",accelX)); 
    nameValuePairs.add(new BasicNameValuePair("accelY",accelY)); 
    nameValuePairs.add(new BasicNameValuePair("accelZ",accelZ)); 

    this.sendData(nameValuePairs); 
} 

所以後來在服務器上的文件AddAccelerationData.php是:

<?php 
/* 
* What this file does is it: 
* 1) Creates connection to database. 
* 2) Retrieve the data being send. 
* 3) Add the retrieved data to database. 
* 4) Close database connection. 
*/ 
require_once '../Connection.php'; //connect to a database/disconnect handler. 
require_once '../SendAPI.php'; //deals with sending querys. 

$server = new Connection(); 
$send = new Send(); 

//Connect to database. 
$server->connectDB(); 

//Retrieve the data. 
$userID = $_POST['userID']; 
$date = $_POST['date']; 
$time = $_POST['time']; 

$accelX = $_POST['accelX']; 
$accelY = $_POST['accelY']; 
$accelZ = $_POST['accelZ']; 

//Add data to database. //Personal method to query and add to database. 
$send->sendAccelerationData($userID, $date, $time, $timeStamp, $accelX, $accelY, $accelZ); 


//Disconnect from database. 
$server->disconnectDB(); 
?> 

這是我最近使用的一個例子。只需在php文件中註明。我導入Connection.php 這只是處理與數據庫的連接。所以,只需用代碼連接到MYSQL數據庫即可。另外我導入了SendAPI.php(你可以忽略)這只是我發送數據的類。基本上它包含了我想用的一些查詢。比如sendAccelerationData()。基本上類與存儲過程類似。

+0

先生我做了相同的代碼,但我想運行我的應用程序localhost,只是爲了測試。我把我的頁面地址設置爲「http:(我的ip)/filename.php」,但我得到了「http/1.1 403 Forbidden」的迴應,你能幫我多一些,請........ .. – Sahi

+0

@BobMcboberson你有沒有用過任何aspx頁面的方法? –