简体   繁体   English

如何在Android中使用JsonObjectRequest通过POST在PHP中接收json

[英]How to receive json via POST in PHP using JsonObjectRequest in Android

I am new to PHP. 我是PHP新手。

I am sending json using JsonObjectRequest in Android via POST Method. 我正在通过POST方法在Android中使用JsonObjectRequest发送json

I don't know how to retrieve that json in php and decode it. 我不知道如何在php中检索该json并将其解码。 Could you help me? 你可以帮帮我吗? The json I am sending is 我正在发送的json是

{"firstname":"test","lastname":"test"}

I need both the JSON objects in seperate variables. 我需要两个JSON对象都在单独的变量中。

You can get this data by file_get_contents("php://input"); 您可以通过file_get_contents("php://input");获得此数据file_get_contents("php://input"); . And then decode it by json_decode() . 然后通过json_decode()对其进行解码。 Try following code: 尝试以下代码:

$data_json = file_get_contents("php://input");
$data = json_decode($data_json,true);
var_dump($data); //show your data

EDIT 编辑

To echo your data: 要回显您的数据:

echo $data['firstname'];
echo $data['lastname'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM