做Android的登录和注册时响应不包含任何数据 [英] Response does not contain any data when doing Android login and registration

查看:357
本文介绍了做Android的登录和注册时响应不包含任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android手机应用程序登录和注册到MySQL,PHP和SQLite。我指的http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/.不过,我面临着一些问题,因为PHP不断提醒我mysql_connect的德precation()的。因此,我不得不切换所有MySQL的语法MySQLi的语法。并成功建立连接。当我通过在数据标签,姓名,电子邮件地址和密码的PHP文件,该文件是负责处理此数据并返回JSON响应,它显示的响应选项卡下:响应不包含任何数据。

这是采用先进的REST客户端应用程序来测试反应的结果页面:

我的PHP文件,该文件要处理数据:

\r
\r

< PHP\r
 \r
/ **\r
 *文件来处理所有的API请求\r
 *接受GET和POST\r
 *\r
 *每个请求将被标记进行标识\r
 *反应将是JSON数据\r
 \r
  / **\r
 *检查POST请求\r
 * /\r
如果(使用isset($ _ POST ['标签'])及和放大器;!$ _ POST ['标签'] =''){\r
    //获取标签\r
    $标签= $ _ POST ['标签'];\r
 \r
    //包括数据库处理程序\r
    require_once'包括/ DB_Functions.php';\r
    $ DB =新DB_Functions();\r
 \r
    //响应阵列\r
    $响应=阵列();\r
 \r
    //检查标签类型\r
    如果($标记=='登录'){\r
        //请求的类型是检查登录\r
        $电子邮件= $ _ POST [电子邮件];\r
        $密码= $ _ POST ['密码'];\r
 \r
        //检查用户\r
        $ USER = $ DB-GT&; getUserByEmailAndPassword($电子邮件,$密码);\r
        如果($用户!= FALSE){\r
            //用户发现\r
            $响应[错误] = FALSE;\r
            $响应[UID] = $用户[UNIQUE_ID];\r
            $响应[用户] [名称] = $用户[名];\r
            $响应[用户] [电子邮件] = $用户[电子邮件];\r
            $响应[用户] [created_at] = $用户[created_at];\r
            $响应[用户] [的updated_at] = $用户[的updated_at];\r
            回声json_en code($响应);\r
        }其他{\r
            //用户未找到\r
            //回声JSON,错误= 1\r
            $响应[错误] = TRUE;\r
            $响应[ERROR_MSG] =​​!不正确的电子邮件或密码;\r
            回声json_en code($响应);\r
        }\r
    }否则如果($标记=='注册'){\r
        //请求的类型是注册新用户\r
        $名称= $ _ POST ['名'];\r
        $电子邮件= $ _ POST [电子邮件];\r
        $密码= $ _ POST ['密码'];\r
 \r
        //检查用户是否已经存在\r
        如果($ DB-GT&; isUserExisted($电子邮件)){\r
            //用户已经存在 - 错误响应\r
            $响应[错误] = TRUE;\r
            $响应[ERROR_MSG] =​​用户已经存在;\r
            回声json_en code($响应);\r
        }其他{\r
            //存储用户\r
            $ USER = $ DB-GT&; storeUser($名称,$电子邮件,$密码);\r
            如果($用户){\r
                //用户成功存储\r
                $响应[错误] = FALSE;\r
                $响应[UID] = $用户[UNIQUE_ID];\r
                $响应[用户] [名称] = $用户[名];\r
                $响应[用户] [电子邮件] = $用户[电子邮件];\r
                $响应[用户] [created_at] = $用户[created_at];\r
                $响应[用户] [的updated_at] = $用户[的updated_at];\r
                回声json_en code($响应);\r
            }其他{\r
                //用户未能保存\r
                $响应[错误] = TRUE;\r
                $响应[ERROR_MSG] =​​错误的发生Registartion;\r
                回声json_en code($响应);\r
            }\r
        }\r
    }其他{\r
        //用户未能保存\r
        $响应[错误] = TRUE;\r
        $响应[ERROR_MSG] =​​不明'标签'的价值应该是'登录'或'注册';\r
        回声json_en code($响应);\r
    }\r
}其他{\r
    $响应[错误] = TRUE;\r
    $响应[ERROR_MSG] =​​所需的参数'标签'消失了!\r
    回声json_en code($响应);\r
}\r
?>

\r

\r
\r

有没有人有关于如何解决这个想法?


解决方案

您回应阵列更改

  $响应=阵列(标签=> $标记,错误=> FALSE);

我刚刚换了同样来的mysqli和它的作品。

I am developing an Android mobile app login and registration to MySQL, PHP and SQLite. I am referring to http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/. However I am facing some problem as the PHP keeps reminding me of the deprecation of mysql_connect(). So I had switch all the MySQL syntax to MySQLi syntax. And the connection is successfully established. When I pass in the data: "tag,name,email and password" The PHP file which is responsible for processing this data and return in JSON response, under the Response tab it's shown: Response does not contain any data..

This is the result page using Advanced Rest Client Application to test the response:

My php file which should process the data:

<?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);
}
?>

Does anyone have idea about how to solve this?

解决方案

Change your response array to

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

I've just changed the same to MYSQLI and it works.

这篇关于做Android的登录和注册时响应不包含任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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