[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.