使用JSON / PHP从android接收错误响应

时间:2013-03-15 14:41:17

标签: php android json phpmyadmin jsonresult

我正在编写我的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;
    }
}

我真的无法弄清楚这里有什么问题,因为早些时候我可以运行得很好但现在我修改此代码以将详细信息存储在论坛的用户表中,这样我就可以处理一个用户数据表 我对这里的新代码感到非常满意 - 我的意思是我认为这不是错误响应的原因 - 但是我对表有疑问但是我不知道哪个方法是错误触发错误响应

1 个答案:

答案 0 :(得分:0)

好的家伙现在我修好了没问题 它是我的android端的函数,它发出登录请求,它的参数没有设置为匹配我的php文件中的函数的顺序。 非常感谢老兄 特别感谢karmafunk你帮我做了很多你最好的

希望我的问题可以帮助其他同样的问题。