插入到 mysql 表中,但为什么json_encode返回 null


Insert into mysql table, but why json_encode return null?

为什么我的应用程序在数据库中正确输入值,但 json 返回给我的 android 应用程序的值为 NULL?

问题出在网络上,但这在浏览器中工作正常。

函数 json_last_error () 返回 0。

我的 php 代码:

<?php
$usuario = $_POST['usuario'];
$passw = $_POST['password'];
$cpassw = $_POST['cpassword'];
$res = 0;
require_once 'funciones_bd.php';
$db = new funciones_BD();
if($db->isuserexist($usuario,$passw)){
    // echo(" Este usuario ya existe ingrese otro diferente!");
    $res = 1;       
}else{
    if($db->adduser($usuario,$passw)) { 
        // echo(" El usuario fue agregado a la Base de Datos correctamente.");          
        $res = 2;               
    }else{
        // echo(" ha ocurrido un error.");
        $res = 3;
    }
}
if ($res==2) {
    $resultado[]=array("logstatus"=>"1");
}
else {
    $resultado[]=array("logstatus"=>"0");
}
echo json_encode($resultado);
echo json_last_error();
?>

我的日志猫:

10-03 18:59:43.991: I/Choreographer(5426): Skipped 32 frames!  The application may be doing too much work on its main thread.
10-03 18:59:50.261: D/InputEventConsistencyVerifier(5426): KeyEvent: ACTION_UP but key was not down.
10-03 18:59:50.261: D/InputEventConsistencyVerifier(5426):   in android.widget.EditText{4110cd60 VFED..CL .F....I. 15,497-465,546 #7f050004 app:id/edpassword}
10-03 18:59:50.261: D/InputEventConsistencyVerifier(5426):   0: sent at 27274938000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=27274938, downTime=27274788, deviceId=0, source=0x101 }
10-03 18:59:53.090: D/InputEventConsistencyVerifier(5426): KeyEvent: ACTION_UP but key was not down.
10-03 18:59:53.090: D/InputEventConsistencyVerifier(5426):   in android.widget.EditText{4110f430 VFED..CL .F....ID 15,575-465,624 #7f050007 app:id/reppassword}
10-03 18:59:53.090: D/InputEventConsistencyVerifier(5426):   0: sent at 27277780000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=27277780, downTime=27277671, deviceId=0, source=0x101 }
10-03 18:59:57.960: D/dalvikvm(5426): GC_CONCURRENT freed 502K, 9% free 6389K/7016K, paused 136ms+182ms, total 661ms
10-03 18:59:58.300: E/getpostresponse(5426):  result= <br />
10-03 18:59:58.300: E/getpostresponse(5426): <font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
10-03 18:59:58.300: E/getpostresponse(5426): <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: cpassword in C:'wamp'www'android'adduser.php on line <i>5</i></th></tr>
10-03 18:59:58.300: E/getpostresponse(5426): <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
10-03 18:59:58.300: E/getpostresponse(5426): <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
10-03 18:59:58.300: E/getpostresponse(5426): <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0000</td><td bgcolor='#eeeeec' align='right'>142712</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:'wamp'www'android'adduser.php' bgcolor='#eeeeec'>..'adduser.php<b>:</b>0</td></tr>
10-03 18:59:58.300: E/getpostresponse(5426): </table></font>
10-03 18:59:58.300: E/getpostresponse(5426): [{"logstatus":"1"}]0
10-03 18:59:58.300: E/log_tag(5426): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray
10-03 18:59:58.300: D/JSON(5426): null
10-03 18:59:59.264: E/JSON(5426): ERROR
10-03 18:59:59.300: E/onPostExecute=(5426): err
10-03 18:59:59.580: I/Choreographer(5426): Skipped 35 frames!  The application may be doing too much work on its main thread.
10-03 19:00:07.201: W/IInputConnectionWrapper(5426): showStatusIcon on inactive InputConnection

你的应用收到 XDebug 错误消息和以下值:

[{"logstatus":"1"}]0

这不是有效的 JSON,因为它的末尾有一个0。因此,你的应用无法将其解析为 JSON 数组。

首先,您需要解决PHP问题。显然cpassword不在 POST 请求中。也许您需要添加它,或者您可能需要在尝试使用它之前检查它是否存在。

然后,尝试注释掉 PHP 的这一行:

echo json_last_error();

然后,你的应用应该能够正确地将响应解析为 JSON。