2012-04-11 50 views
1

我想上傳一個jsonarray到服務器,然後得到響應文本來查看服務器對對象做了什麼。在Android方面,我有這樣的代碼:如何讓php代碼顯示我要發送給服務器的內容?

  HttpPost httppost = new HttpPost("http://10.0.0.2/namedate.php");I am 
      HttpClient httpclient = new DefaultHttpClient(); 
      httppost.getParams().setParameter("jsonarray", json_a.toString()); 
      HttpResponse response = httpclient.execute(httppost); 
      String responseText = EntityUtils.toString(response.getEntity()); 
      Log.d("ProviderTester", "The response text is "+ responseText); 
      Log.i("JSONInfo","JSON object: " + json_a.toString()); 

的jsonarray看起來像這樣從logcat的:

04-10 21:29:53.293: I/JSONInfo(466): JSON object: ["[name=Mike, datetime=2012-04-10 21:29]","[name=Roger, datetime=2012-04-10 21:29]"] 

目前我只是想呼應的字符串,然後希望以後獲得表出來的字符串:

<?php 

    echo $_POST['jsonarray']; 

    ?> 

下面是我從logcat中得到迴應:

04-10 22:22:20.033: D/ProviderTester(499): The response text is 

如何解決這個問題,以便我可以看到發送給服務器的jsonarray字符串?

編輯:當我改變我的Android代碼到:

  ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("json_a", json_a.toString())); 

      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      String responseText = EntityUtils.toString(response.getEntity()); 

然後我得到的logcat以下響應使用公認的答案爲PHP腳本:

04-10 23:05:39.833: D/ProviderTester(601): The response text is POST = array (
04-10 23:05:39.833: D/ProviderTester(601): 'json_a' => '[name=Mike, datetime=2012-04-10 21:29]\\",\\"[name=Roger, datetime=2012-03-10 21:29]\\"]\\"]', 
04-10 23:05:39.833: D/ProviderTester(601):) 
04-10 23:05:39.833: D/ProviderTester(601): GET = array (
04-10 23:05:39.833: D/ProviderTester(601):) 
04-10 23:05:39.833: D/ProviderTester(601): request = array (
04-10 23:05:39.833: D/ProviderTester(601): 'Content-Length' => '174', 
04-10 23:05:39.833: D/ProviderTester(601): 'Content-Type' => 'application/x-www-form-urlencoded', 
04-10 23:05:39.833: D/ProviderTester(601): 'Host' => 'graasdfon.hostei.com', 
04-10 23:05:39.833: D/ProviderTester(601): 'Connection' => 'Keep-Alive', 
04-10 23:05:39.833: D/ProviderTester(601): 'User-Agent' => 'Apache-HttpClient/UNAVAILABLE (java 1.4)', 
04-10 23:05:39.833: D/ProviderTester(601): 'Expect' => '100-Continue', 
04-10 23:05:39.833: D/ProviderTester(601):) 
04-10 23:05:39.833: D/ProviderTester(601): 
04-10 23:05:39.833: D/ProviderTester(601): <!-- www.000webhost.com Analytics Code --> 

現在我只需要弄清楚如何處理json_a服務器端。謝謝您的幫助!

回答

0

回聲$ _ POST [ 'jsonarray'];

PHP沒有辦法做非標量數據類型的隱式toString()表示。你需要做一個明確的轉換。

例如如果你想看到所有的HTTP變量和請求頭,你可以試試:

<?php 

$out="POST = " . var_export($_POST, true) . "\n"; 
$out.="GET = " . var_export($_GET, true) . "\n"; 
$out.="request = " . var_export(getallheaders(), true) . "\n"; 
print $out; 

?> 

(這不會挑上寫到標準輸入的內容,但不能正確encloded作爲POST)

+0

感謝您的幫助!任何想法如何從json_a服務器端獲取信息? – Stagleton 2012-04-12 14:15:26

+0

http://php.net/manual/en/function.json-decode.php – symcbean 2012-04-13 08:02:06

1

我不知道是什麼原因造成您的問題(我不知道的Android/Java的),但調試這將是簡單地在這個PHP的最佳方法:

<?php 
    var_dump($_POST); 
?> 

這將導致PHP輸出通過POST收到的所有內容。

0

您需要在帖子的實體中傳遞json值。試試這個:

StringEntity params =new StringEntity(json_a.toString()); 
httppost.addHeader("content-type", "application/x-www-form-urlencoded"); 
httppost.setEntity(params); 

相反的:

httppost.getParams().setParameter("jsonarray", json_a.toString()); 
+0

嗯,我嘗試了使用所有不同的PHP配置的方法,響應文本是:array(0){} – Stagleton 2012-04-12 13:51:47

+0

使用Fiddler檢查請求是否正確發送。 – SiN 2012-04-12 15:08:51

相關問題