gpt4 book ai didi

mysql - 无法将 json 从 android 插入 mysql

转载 作者:行者123 更新时间:2023-11-29 23:01:04 25 4
gpt4 key购买 nike

一些帮助,谢谢。我的 Android 应用程序发送一个像 json

[{"userName":"fred","userId":"1"},{"userName":"juma","userId":"2"},     {"userName":"drer","userId":"3"},{"userName":"drer","userId":"4"},{"userName":"sdd","userId":"5"}] 

到我的 php 5.3 服务器。当我在代码中使用字符串时,它在注释掉 json_encode 后工作得很好,但是当我使用 file_get_contents 时它什么也不做,当我取消注释 json_encode 时它返回 NULL ..我做错了什么。我的服务器主机使用的是 php 5.3.28。这是我的代码..

<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();

//Get JSON posted by Android Application
$json = file_get_contents('php://input');


//$json ='[{"userName":"fred","userId":"1"},{"userName":"juma","userId":"2"},{"userName":"drer","userId":"3"},{"userName":"drer","userId":"4"},{"userName":"sdd","userId":"5"}]';

//$json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t](//).*)#", '', $json);

//$json = json_encode($json);
$data = json_decode($json, true);

//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);


?>

错误是...

Notice: Uninitialized string offset: 0 in XXXX on line 28    

Notice: Trying to get property of non-object in xxxxxx on line 28

Notice: Uninitialized string offset: 0 in xxxxxx on line 28

Notice: Trying to get property of non-object in xxxxxx on line 28

Notice: Uninitialized string offset: 0 in xxxxx on line 32

Notice: Trying to get property of non-object in xxxxx on line 32
[{"id":null,"status":"yes"}]

注意..第 28 行是

$res = $db->storeUser($data[$i]->userId, $data[$i]->userName);

第 32 行是

$b["id"] = $data[$i]->userId;

当我回显 $json 时,这是我的 logcat 的一部分

    49-949/com.qnatz.ppolocations I/System.out﹕ [{"userName":"sorry","userId":"1"},{"userName":"wakeup","userId":"2"},{"userName":"sleeping","userId":"3"}]
02-13 12:32:17.110 949-949/com.qnatz.ppolocations W/EGL_emulation﹕ eglSurfaceAttrib not implemented
02-13 12:32:18.070 949-949/com.qnatz.ppolocations I/System.out﹕ usersJSON=%5B%7B%22userName%22%3A%22sorry%22%2C%22userId%22%3A%221%22%7D%2C%7B%22userName%22%3A%22wakeup%22%2C%22userId%22%3A%222%22%7D%2C%7B%22userName%22%3A%22sleeping%22%2C%22userId%22%3A%223%22%7D%5D
02-13 12:32:18.100 949-949/com.qnatz.ppolocations W/System.err﹕ org.json.JSONException: Value usersJSON of type java.lang.String cannot be converted to JSONArray
02-13 12:32:18.100 9

最佳答案

用这个代码解决了这个问题...

<?php
//error_reporting(E_ALL);
//ini_set('display_errors', 1);
include_once './db_functions.php';
//Create Object for DB_Functions clas
$db = new DB_Functions();

//Get JSON posted by Android Application
//$json = file_get_contents('php://input');

$json = urldecode(utf8_decode(stripslashes($_REQUEST['usersJSON'])));

//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);

?>

关于mysql - 无法将 json 从 android 插入 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28493997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com