php的post请求中缺少json数组


Missing json array in post request to php

我试图将json对象从Android设备发送到PHP以插入数据库,但服务器找不到。我做错了什么?谢谢

脚本在isset检查中失败。

PHP脚本:

    <?php
$json_array = json_decode($_POST['messages']) -> messages;
if(isset($json_array))
{
    //connect to database and do insertions.
} else
{
    echo "Not found array with messages in POST request'n";
}

后请求包含:

{"messages":[{"message":"Hello. Test from ukraine","id":1,"from_sender":"1111-111-1111","box":"2","type":"sms"}]}

它是这样形成的:

HttpClient httpClient = new DefaultHttpClient();
            try {
                HttpPost httpPost = new HttpPost(BASE_URL + "/josyko.php");
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("messages", request.toString()));
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse httpResponse = httpClient.execute(httpPost);
                result = EntityUtils.toString(httpResponse.getEntity());
            } catch (IOException e) {
                e.printStackTrace();
            }

您可以使用:http://php.net/manual/en/reserved.variables.httprawpostdata.php

$json_array = json_decode($HTTP_RAW_POST_DATA) -> messages;

或者更好的解决方案:

file_get_contents('php://input'); // instead of $HTTP_RAW_POST_DATA