【问题标题】:Undefined index: usersJSON on line 6 []未定义索引:第 6 行的 usersJSON []
【发布时间】:2014-11-17 05:14:57
【问题描述】:

我正在尝试获取 Android 应用程序发布的 JSON,然后将 JSON 解码为一个数组,但得到:未定义索引:第 6 行的 usersJSON []

//Get JSON posted by Android Application
$json = $_POST["usersJSON"]; // Undefined index

php 脚本如下所示:

<?php
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions(); 
//Get JSON posted by Android Application
$json = $_POST["usersJSON"]; // Undefined index
//Remove Slashes
if (get_magic_quotes_gpc()){
$json = stripslashes($json);
}
//Decode JSON into an Array
$data = json_decode($json);
//Util arrays to create response JSON
$a=array();
$b=array();
//Loop through an Array and insert data read from JSON into MySQL DB
for($i=0; $i<count($data) ; $i++)
{
//Store User into MySQL DB
$res = $db->storeUser($data[$i]->userId,$data[$i]->userName);
    //Based on inserttion, create JSON response
    if($res){
        $b["id"] = $data[$i]->userId;
        $b["status"] = 'yes';
        array_push($a,$b);
    }else{
        $b["id"] = $data[$i]->userId;
        $b["status"] = 'no';
        array_push($a,$b);
    }
}
//Post JSON response back to Android Application
echo json_encode($a);
?>

【问题讨论】:

  • 未定义的索引意味着您没有该索引的值

标签: php android mysql arrays json


【解决方案1】:

看起来$_POST["usersJSON"] 未设置或为空(您没有此索引的值)

检查您是否在 url 上发帖

usersJSON='yourjsondata'

对于隐藏未定义索引错误,您需要先通过isset()empty() 进行检查

$json = (isset($_POST["usersJSON"])? $_POST["usersJSON"] : ''); 

或检查是否为空

if(!empty($_POST["usersJSON"]) {
   $json = $_POST["usersJSON"];
 }
 else {
  echo 'getting blank'; 
 }

【讨论】:

  • 现在我在这一行得到 [ ]:$data = json_decode($json);未定义变量:json
  • 您没有收到发布数据,能否请您发布您的代码如何发布数据
【解决方案2】:

看起来您的应用程序正在以不同的名称发布或根本不发布(您是发布信息还是获取信息?)

您可以尝试使用 var_dump($_POST) 转储所有 $_POST,以便了解应用程序发布的内容(如果有)。如果此数组为空,则表示它不是作为“POST”发送的。您可以检查 $_GET 数组...如果是这种情况,您可能需要修复 Android 应用程序以正确发送发布请求:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-01
    • 2018-10-11
    • 2012-02-11
    • 1970-01-01
    • 2021-02-13
    • 2023-03-20
    相关资源
    最近更新 更多