2012-02-24 142 views
1

我目前正在開發Android應用程序,它將使用Web服務。我使用PHP作爲後端。我目前正在嘗試通過JSON身份驗證到PHP。但我堅持某一點,希望你們會幫助。在PHP中處理JSON POST請求

我成功地編寫代碼來創建JSON數據在android中也使用mysql的數據庫連接在PHP中,但我很困惑如何處理JSON數據。我正在使用POST請求發送JSON數據。

我喜歡問我如何處理PHP中的JSON數據。更具體地說,我想知道如何在PHP中獲取包含JSON數據的POST請求?

在此先感謝。 感謝你。

編輯: 我使用發送android系統中

    HttpPost post = new HttpPost(address); 
      json.put("username", username); 
     json.put("password", pwd); 
     StringEntity se = new StringEntity("json"+json.toString()); 
     Log.i(DEB_TAG, "The JSON Request is:"+json.toString()); 
     se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,   "application/json")); 
     post.setEntity(se); 
     Log.i(DEB_TAG, "The post request is "+post.toString()); 
     response = client.execute(post); 

     if(response != null){ 
      InputStream in = response.getEntity().getContent(); 
      Log.i(DEB_TAG, "The result is"+in.toString()); 
      } 

POST請求,並使用下面的代碼在PHP解析JSON請求下面的代碼:

$string = $_POST['josnHeader']; 
$obj = json_decode($string); 
$username = $obj->{'username'}; 
$password = $obj->{'password'}; 

它是正確的還是我做的任何錯誤的實現?

回答

2

你看過PHP中的$ _POST數組嗎?

+0

雅阿,它示出了$ _POST陣列空 – ashutosh 2012-02-24 14:16:43

1
$json = $_POST["var_name"]; 
$array = json_decode($json); 
0
$json = $_REQUEST["your_param"]; 
$dtoObject = json_decode(stripslashes($json),true);