1

嗨,我向 php 服务器发送 JSON 对象的每个人。我的 json 如下 -

        {"cart_detail":{"product_id":"66922","ref_id":"chand09","user_id":"1"}}

我通过以下代码发送 JSON-

      public void postData(String result,JSONObject obj) {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, 10000);
    HttpConnectionParams.setSoTimeout(myParams, 10000);
    System.out.println(" before JSON string");
    String json=obj.toString();

    try {

        HttpPost httppost = new HttpPost(result.toString());
        httppost.setHeader("Content-type", "application/json");
        System.out.println("After httpost header");
        StringEntity se = new StringEntity(obj.toString());   
        System.out.println(" sending json string"+se);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se); 

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        String temp = EntityUtils.toString(response.getEntity());

          System.out.println(" i am getting response entity "+entity);


        Log.i("tag", temp);
        System.out.println(" item send sucessfully to cart by server");

    } catch (ClientProtocolException e) {

    } catch (IOException e) {
    }
}

我处理响应的 PHP 是-

      $data = array("cart_details" => array($_REQUEST));
       echo "<pre>";
       print_r($data);
       echo "</pre>";

作为回应,我得到堆栈跟踪如下 -

    07-26 14:57:45.124: I/System.out(7191):  i am getting response entity org.apache.http.conn.BasicManagedEntity@405d2fd0
    07-26 14:57:45.124: I/System.out(7191):  i am getting response string org.apache.http.impl.client.ClientParamsStack@405134a8
   07-26 14:57:45.124: I/tag(7191): <pre>Array
   07-26 14:57:45.124: I/tag(7191): (
   07-26 14:57:45.124: I/tag(7191):     [cart_details] => Array
   07-26 14:57:45.124: I/tag(7191):         (
  07-26 14:57:45.124: I/tag(7191):             [0] => Array
  07-26 14:57:45.124: I/tag(7191):                 (
  07-26 14:57:45.124: I/tag(7191):                 )
   07-26 14:57:45.124: I/tag(7191):         )
   07-26 14:57:45.124: I/tag(7191): )
   07-26 14:57:45.124: I/tag(7191): </pre>
  07-26 14:57:45.124: I/System.out(7191):  item send sucessfully to cart by server

我不知道发生了什么问题。我是 json 的新手。我将如何检查我的请求是否进行。作为回应,我收到了相同的 json,但为什么这个 json 是空的?是 android 部分或 PHP Server 的故障。在此先感谢大家。

4

2 回答 2

1

您正在发送 JSON 编码的 POST 正文,因此您必须从输入流中读取:

$data = array("cart_details" => json_decode(file_get_contents('php://input')),
echo "<pre>";       
print_r($data);
echo "</pre>";

$_REQUEST 和 $_POST 数组只会填充 urlencoded 表单数据。

于 2013-07-26T10:15:27.670 回答
-1

您必须在 PHP 中解码 JSON 字符串,以便在 PHP 中正确理解

$data = json_decode($_REQUEST);
echo '<pre>' . print_r($data, true) . '</pre>';

我希望 json 数据作为命名数组项传递给 PHP $_RESUEST/$_POST/$_GET 数组。最好只打印整个 $_REQUEST 数组以查看它的实际调用情况。

echo '<pre>' . print_r($REQUEST, true) . '</pre>';

然后将正确的数组项添加到原始转储中

$data = json_decode($_REQUEST['array_item_name']);
echo '<pre>' . print_r($data, true) . '</pre>';
于 2013-07-26T09:58:38.870 回答