简体   繁体   English

如何使用POST方法从android接收json对象

[英]how to receive json object from android using POST method

I am working on a php script which receives data sent by android in json format using POST method. 我正在研究一个php脚本,该脚本使用POST方法接收json格式的android发送的数据。 But i am not getting any data, here is my code 但是我没有任何数据,这是我的代码

$josn = file_get_contents("php://input");   
$json_data = json_decode($josn, true);   

i just tried to print $json_data by 我只是试图通过打印$ json_data

print_r($json_data);   

but its not showing any values 但它没有显示任何值

var_dump() is much more reliable than print_r() var_dump()print_r()更可靠

Whether the data is corrupt or not, it will tell you 'something' about the thing you are trying to see. 无论数据是否损坏,它都会告诉您有关您尝试查看的内容的“信息”。

If it is null it will say ' null '. 如果为null ,则会说' null '。

If it is ' bob ' it will say (string 3) 'bob' . 如果是' bob ',则会说(字符串3)'bob'

If it is 4 it will say (int) 4 如果是4 ,它将说(int)4

If it is ' 4 ' it will say (string 1) '4' 如果它是' 4 ',它将说(字符串1)'4'

Also, your json_decode() might be breaking if the input is not valid json. 另外,如果输入无效的json,则json_decode()可能会中断。 A safer way to see if you are getting any data at all is to dump and check your data before you pass it into the json_decode() function: 一种更安全的方法来查看是否获取任何数据是转储并检查数据,然后再将其传递给json_decode()函数:

var_dump( file_get_contents("php://input") );

If you see valid json, then it is safe to pass your $josn variable into the json_decode() function. 如果看到有效的json,则可以安全地将$ josn变量传递到json_decode()函数中。

Bonus 奖金

Seems dumb but it's worth mentioning that you can only use file_get_contents() on the input buffer once and then the contents are gone. 似乎很愚蠢,但值得一提的是,您只能在输入缓冲区上使用file_get_contents()一次,然后内容就消失了。 If you try to read them again, they will be null. 如果您尝试再次阅读它们,它们将为null。 Refer to the variable you stored them in when you first read them. 首次阅读它们时,请参考存储它们的变量。

Separetely 分开地

Another fellow mentioned checking your global $_POST[] variable... which brings up a great point. 另一个人提到检查您的全局$ _POST []变量...这很有意义。 Headers! 标头!

For json pickup with file_get_contents('php://input') , php needs your android app to send the header: 对于使用file_get_contents('php://input') json拾取,php需要您的android应用发送标头:

header('Content-Type: application/json');

For POST variable pickup with $_POST[] , php needs your android app to send the header: 对于使用$_POST[]进行POST变量提取,php需要您的android应用发送标头:

header('Content-Type: application/x-www-form-urlencoded');

OR 要么

header('Content-Type: multipart/form-data');

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

相关问题 使用 Volley 库从 Android 在 PHP 服务器中接收 Json 数据 POST 方法 - Receive Json data POST method in PHP server from Android using Volley Library 如何使用POST方法从Android获取php的Json DATA? - How to get Json DATA with php from Android using POST method? 如何在Android中使用JsonObjectRequest通过POST在PHP中接收json - How to receive json via POST in PHP using JsonObjectRequest in Android 如何从android活动中的php接收多行json对象? - How to receive the multiple rows of json object from php in android activity? 使用 POST 方法和 HttpURLConnection 将 JSON 对象从 Android 发送到 PHP 服务器 - Send a JSON Object from Android to PHP server with POST method and HttpURLConnection 如何使用httpClient和JSON从PHP到Android接收数组? - How to receive an array from php to android using httpClient and JSON? 我的PHP文件没有使用POST方法从我的Android应用程序接收数据 - My PHP file doesn't receive data from my android app using POST method 如何在JSON对象中使用POST方法在Laravel中获取数据? - How to get data in Laravel using POST method in JSON Object? 如何从PHP的Android应用程序的post方法解码json数据? - How to decode json data from post method for android app in php? 如何将json对象发布到Web API,然后接收响应? - How to post json object to web api and then receive response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM