Android错误:java.lang.String类型的值< br无法转换为JSONObject [英] Android Error: Value <br of type java.lang.String cannot be converted to JSONObject

查看:200
本文介绍了Android错误:java.lang.String类型的值< br无法转换为JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我创建具有登录功能的应用程序.为了从android连接到MySQL数据库,我使用PHP.当我使用MySQLi时,一切正常.但是,当我转换为PDO时,该错误将与我的问题的标题相同.谁能知道这是什么问题吗?下面是我的PHP代码:

Currently, I create an apps with a login function. To connect from android to MySQL database, I use PHP. When I use MySQLi, everything is okay. But when I convert to PDO, The error will appear the same as my question's title. Can anyone knows what is the problem? Below is my PHP code:

<?php 

    require_once 'configPDO.php';

    $response = array();

        if(isTheseParametersAvailable(array('badgeid', 'pwd'))){

            $badgeid = $_POST['badgeid'];
            $pwd = $_POST['pwd']; 

            $stmt = $conn->prepare("SELECT badgeid, email, fullname, roles_id, team_id FROM users WHERE badgeid = :badgeid AND pwd = :pwd AND roles_id = 3");
            // $stmt->bind_param("ss",$badgeid, $pwd);

            $stmt->bindParam(':badgeid',$badgeid,PDO::PARAM_STR);
            $stmt->bindParam(':pwd',$pwd,PDO::PARAM_STR);
            $stmt->execute();

            //$stmt->store_result();

            if($stmt->rowCount() > 0){

                $stmt->bindParam($badgeid, $email, $fullname, $roles_id, $team_id);
                $stmt->fetch();

                $user = array(
                    'badgeid'=>$badgeid, 
                    'email'=>$email,
                    'fullname'=>$fullname,
                    'roles_id'=>$roles_id,
                    'team_id'=>$team_id
                );

                $response['error'] = false; 
                $response['message'] = 'Login successfull'; 
                $response['user'] = $user; 
            }else{
                $response['error'] = false; 
                $response['message'] = 'Invalid username or password';
            }
        }
    echo json_encode($response);

    function isTheseParametersAvailable($params){

        foreach($params as $param){
            if(!isset($_POST[$param])){
                return false; 
            }
        }
        return true; 
    }

推荐答案

if条件内的第二个bindParam()(您应该阅读并理解此方法的确切作用!)是胡说八道!

Your second bindParam() (You should read and understand what exactly this method do!) inside the if condition is nonsense!

更改此:

if($stmt->rowCount() > 0){

    $stmt->bindParam($badgeid, $email, $fullname, $roles_id, $team_id);
    $stmt->fetch();

    $user = array(
        'badgeid'=>$badgeid, 
        'email'=>$email,
        'fullname'=>$fullname,
        'roles_id'=>$roles_id,
        'team_id'=>$team_id
    );

对此:

$result = $stmt->fetch(\PDO::FETCH_ASSOC); // Get results as array
if ($result) {
    // Since we only get the fields we want to send back, you can assign `$result` directly to `$response['user']`
     $response['user'] = $result; 

PHP引发了一个相关的错误,您将在请求的原始响应中看到该错误!

PHP had thrown an related error, which you would have seen in the raw response of you request!

这篇关于Android错误:java.lang.String类型的值&lt; br无法转换为JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆