2013-10-03 47 views
0

为什么我的应用程序正确输入数据库中的值,但json返回给我的android应用程序的值是NULL?插入到mysql表中,但为什么json_encode返回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 

回答

0

您的应用正在接收的XDebug错误消息,并且该值:

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

这是而不是有效的JSON,因为它的末尾有0。所以你的应用程序无法将其解析为JSON数组。

首先,您需要修复该PHP问题。显然cpassword不在POST请求中。也许你需要添加这个,或者也许你需要检查它是否存在,然后再尝试使用它。

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

echo json_last_error(); 

那么你的应用程序应该能够正确地解析响应为JSON。

+0

添加评论 // $ cpassw = $ _POST ['cPassword']; 一切正常。 所以在java应用程序上验证密码是完成的。但是,Web应用程序中的验证如何? –

+0

你的PHP错误是'cpassword'不在'$ _POST'数组中。如果你需要它在那里,你应该检查你的Android应用程序是否添加了这个值,它的关键是实际的密码。 –