未定义索引:第6行[]上的usersJSON

时间:2014-11-17 05:14:58

标签: php android mysql arrays json

我试图通过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);
?>

2 个答案:

答案 0 :(得分:1)

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

检查您是否在网址上发帖

usersJSON='yourjsondata'

对于隐藏未定义的索引错误,您需要先isset()empty()

进行检查
$json = (isset($_POST["usersJSON"])? $_POST["usersJSON"] : ''); 

或检查空

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

答案 1 :(得分:0)

看起来您的应用程序发布到其他名称或根本没有发布(您是发帖还是获取?)

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