php - Android登录注册时响应不包含任何数据

标签 php android mysql sqlite mysqli

我正在开发一个 Android 移动应用程序登录和注册到 MySQL、PHP 和 SQLite。我指的是 http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ .然而,我遇到了一些问题,因为 PHP 不断提醒我 mysql_connect() 的弃用。所以我已经将所有 MySQL 语法切换为 MySQLi 语法。并成功建立连接。当我传入数据时:“标签、名称、电子邮件和密码” 负责处理此数据并以 JSON 响应返回的 PHP 文件在“响应”选项卡下显示:响应不包含任何数据。

这是使用 Advanced Rest Client Application 测试响应的结果页面:

enter image description here

我应该处理数据的 php 文件:

<?php
 
/**
 * File to handle all API requests
 * Accepts GET and POST
 * 
 * Each request will be identified by TAG
 * Response will be JSON data
 
  /**
 * check for POST request 
 */
if (isset($_POST['tag']) && $_POST['tag'] != '') {
    // get tag
    $tag = $_POST['tag'];
 
    // include db handler
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();
 
    // response Array
    $response = array();
 
    // check for tag type
    if ($tag == 'login') {
        // Request type is check Login
        $email = $_POST['email'];
        $password = $_POST['password'];
 
        // check for user
        $user = $db->getUserByEmailAndPassword($email, $password);
        if ($user != false) {
            // user found
            $response["error"] = FALSE;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = TRUE;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    } else if ($tag == 'register') {
        // Request type is Register new user
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];
 
        // check if user is already existed
        if ($db->isUserExisted($email)) {
            // user is already existed - error response
            $response["error"] = TRUE;
            $response["error_msg"] = "User already existed";
            echo json_encode($response);
        } else {
            // store user
            $user = $db->storeUser($name, $email, $password);
            if ($user) {
                // user stored successfully
                $response["error"] = FALSE;
                $response["uid"] = $user["unique_id"];
                $response["user"]["name"] = $user["name"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["created_at"] = $user["created_at"];
                $response["user"]["updated_at"] = $user["updated_at"];
                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = TRUE;
                $response["error_msg"] = "Error occured in Registartion";
                echo json_encode($response);
            }
        }
    } else {
        // user failed to store
        $response["error"] = TRUE;
        $response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
        echo json_encode($response);
    }
} else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameter 'tag' is missing!";
    echo json_encode($response);
}
?>

有没有人知道如何解决这个问题?

最佳答案

将您的响应数组更改为

$response = array("tag" => $tag, "error" => FALSE);

我刚刚将其更改为 MYSQLI,它可以正常工作。

关于php - Android登录注册时响应不包含任何数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28846191/

相关文章:

php - yii2:显示标签而不是 bool 复选框的值

php - 用于分割除 URL 之外的字符串的正则表达式

php - PHP函数获取带路径的递归路径键

android - 在没有 ActionBarSherlock 的情况下在 Actionbar ShareActionProvider 上设置自定义共享图标

php - 基于数据库列的记录过滤无法使用自动完成搜索 PHP

mysql - 未创建 Wordpress usermeta

php - 订阅包并存储它们

android - 我如何在 flutter 中单击交错 GridView 中的项目

java - Android rss 无法解析带有属性的 XML

php - 准备使用php脚本来比较mysql表