0

我正在编写我的 android 应用程序,该应用程序与本地服务器中的 PHP 论坛共享数据库表。我的应用程序用户需要先注册才能开始使用该应用程序,并且我的应用程序中的功能之一是允许这些注册用户向论坛发送/查看帖子和评论,所有这些都是从 Android 应用程序界面完成的,而不是通过将整个论坛呼叫到我的手机中,但我在此任务中使用了 JSON 并有不同的响应。不幸的是,每次我注册时都会收到错误响应,但我不知道为什么

这是我得到的错误响应

03-14 16:14:52.361: E/JSON(400): {"tag":"register","success":0,"error":1,"error_msg":"Error occured in Registartion"}

这是我的 index.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("tag" => $tag, "success" => 0, "error" => 0);

// 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
        // echo json with success = 1
        $response["success"] = 1;
        $response["id"] = $user["unique_id"];
        $response["user"]["name"] = $user["username"];
        $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"] = 1;
        $response["error_msg"] = "Incorrect email or password!";
        echo json_encode($response);
    }
} else if ($tag == 'register') {
    // Request type is Register new user
    $name = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    // check if user is already existed
    if ($db->isUserExisted($email)) {
        // user is already existed - error response
        $response["error"] = 2;
        $response["error_msg"] = "User already existed";
        echo json_encode($response);
    } else {
        // store user
        $user = $db->storeUser($name,  $password, $email);
        if ($user) {
            // user stored successfully
            $response["success"] = 1;
            $response["id"] = $user["unique_id"];
            $response["user"]["name"] = $user["username"];
            $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"] = 1;
            $response["error_msg"] = "Error occured in Registartion";
            echo json_encode($response);
        }
    }
} else {
    echo "Invalid Request";
}
  } else {
echo "Access Denied";
  }
  ?>

这是我的 STOREUSERFUNCTION

 public function storeUser($name, $password, $email) {
    $uuid = uniqid('', true);
    $hash = $this->hashSSHA($password);
    $encrypted_password = $hash["encrypted"]; // encrypted password
    $salt = $hash["salt"]; // salt
    $result = mysql_query("INSERT INTO user(unique_id, username, password, salt, email,  created_at) VALUES('$uuid', '$name',  '$encrypted_password', '$salt', '$email',  NOW())");
    // check for successful store
    if ($result) {
        // get user details
        $id = mysql_insert_id(); // last inserted id
        $result = mysql_query("SELECT * FROM user WHERE id = $id");
        // return user details
        return mysql_fetch_array($result);
    } else {
        return false;
    }
}

我真的不知道这里出了什么问题,因为之前我可以很好地运行它但是现在我修改了这段代码以将详细信息存储在论坛的用户表中,这样我就可以处理一个用户数据表我对新的感觉很舒服这里的代码 - 我的意思是我猜这不是错误响应的原因 - 但我怀疑表格,但无论如何我不确定触发错误响应的错误在哪里

4

1 回答 1

0

好的,伙计们现在我修好了,没问题,这是我的android端发出登录请求的函数,它的参数设置的顺序与我的php文件中函数的顺序不匹配。非常感谢伙计,特别感谢 karmafunk,你帮了我很多,祝你一切顺利

希望我的问题能帮助其他人解决同样的问题。

于 2013-03-16T09:43:40.963 回答