我想上傳一個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服務器端。謝謝您的幫助!
感謝您的幫助!任何想法如何從json_a服務器端獲取信息? – Stagleton 2012-04-12 14:15:26
http://php.net/manual/en/function.json-decode.php – symcbean 2012-04-13 08:02:06