2011-05-30 62 views
2

我想通過Android中的post方法發送數據。但我怎麼能在服務器上接收數據。我的服務器是在PHP中。我在php $ _post []中找到了一個方法。我如何使用它? 我的客戶代碼是php中的post方法

client=new DefaultHttpClient(); 
      HttpPost request=new HttpPost("http://10.0.2.2/php_server/index.php"); 
      HttpEntity entity; 

      try 
      { 
       StringEntity string=new StringEntity("I am android"); 

       entity=string; 
       request.setEntity(entity); 

       HttpResponse response=client.execute(request); 
      } 
      catch(Exception e) 
      { 
       Log.e("socket connection", e.toString()); 
      } 
+2

順便說一句,'$ _post []'不是一種方法......它是一個數組。 – Cristian 2011-05-30 13:22:30

+0

@克里斯蒂安,很好的通知! ''''''是一個運算符,就像在'Java' – Nemoden 2011-05-30 15:01:18

回答

4

這是PHP的基礎。 $_POST是PHP的超全局數組,它存儲使用POST方法發送到特定腳本的數據。

您可以使用它,下面將輸出的任何其他PHP array

代碼$ _ POST所有鍵和相應的值:

foreach ($_POST as $key => $value) { 
    echo $key . ' = ' . var_export($value, true); 
} 

如果你知道你需要哪些關鍵正是,你可以得到它使用[]像這樣:

$my_value = $_POST['my_post_field_sent_by_android_app']; 
+1

中進行調試一樣''var_dump($ _ POST)''可以確保足夠的 – 2011-05-30 13:24:54

+1

。但我更喜歡[var_export](http://php.net/manual/en/function.var-export.php)。不過,它並不像'var_dump'那樣提供信息。 – Nemoden 2011-05-30 13:25:49