1

我正在尝试将 jsonarray 上传到服务器,然后获取响应文本以查看服务器对对象的作用。在android方面,我有这个代码:

            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());

然后我使用接受的 php 脚本答案在 LogCat 中得到以下响应:

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 服务器端。谢谢您的帮助!

4

3 回答 3

1

我不知道是什么导致了您的问题(我不知道 Android/Java),但调试它的最佳方法是在 PHP 中简单地对此进行调试:

<?php
    var_dump($_POST);
?>

这将导致 PHP 输出它通过 POST 接收到的所有内容。

于 2012-04-11T16:07:16.080 回答
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());
于 2012-04-11T16:24:22.390 回答
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;

?>

(这不会拾取写入 STDIN 但未正确包含为 POST 的内容)

于 2012-04-11T16:48:03.697 回答