php - 插入mysql表,但是为什么json_encode返回null?

标签 php android mysql json

为什么我的应用程序在数据库中输入值是正确的,但是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();
?>

我的Logcat:

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。

关于php - 插入mysql表,但是为什么json_encode返回null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19167369/

相关文章:

数据库中的 Php 更新复选框状态

php - 使用隐藏输入类型的 3 个选项进行搜索不起作用

android - 从移动浏览器扫描 Angular2+ 二维码

android - 喷气背包组合。预览不可用

android - 自定义 toast 取消不起作用

php - 用PHP搜索MySQL并在同一页面显示结果

php - FPDF错误: Unsupported image type: image/jpeg

php - 谁能帮我逐行描述这个php代码: functions & recursions

php - 插入/更新查询

php - 根据下拉菜单中选定的选项查看数据库中的数据