简体   繁体   中英

receiving error response from android with JSON/PHP

I'm coding my android app which shares DB tables with PHP forum in my local server. My app users needs to register before he/she can start using the app and one of the functions in my app is to allow those registered users to send/view posts and comments to/of the forum all are done from the Android app interface not by calling the whole forum into my phone but I used JSON in this task with different responses. Unfortunately every time I register I receive an error response, but I could not figure out why

this is the error response that I got

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

and this is my 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";
  }
  ?>

this is my 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;
    }
}

I really could not figure out what is wrong here as earlier I could run it fine but now I modify this code to store the details in user table of the forum so I can deal with one table for user data I feel quite comfortable with the new code here-I mean I guess it's not the cause of the error response- but I doubt with table but any way I'm not sure where is the mistake that triggers the error response

OK guys now I fixed it it's fine no issues it was the function in my android side that makes the login request, it's parameters are not set in order that matches the order of the function in my php file. thanks a lot dude special thanks goes to karmafunk you did help me a lot which you all the best

hope my question would help others with same matter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM