我想創建一個註冊和登錄函數,但我有JSON的問題,因爲它無法存儲數據。無法存儲數據JSON
這是從日誌中的JSON響應我得到: 02-24 08:24:47.878: E/JSON(2017): {"tag":"register","success":0,"error":1,"error_msg":"Error occured in Registartion"}
這是我的PHP代碼來存儲數據:
<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
// get tag
$tag = $_POST['tag'];
// include db handler
require_once 'DB_Function.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["user"]["email"] = $user["email"];
$response["user"]["contact"] = $user["contact_no"];
$response["user"]["created_at"] = $user["year_joined"];
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
$email = $_POST['email'];
$password = $_POST['password'];
$contact = $_POST['contact'];
// 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($email, $contact, $password);
if ($user) {
// user stored successfully
$response["success"] = 1;
$response["user"]["email"] = $user["email"];
$response["user"]["contact"] = $user["contact_no"];
$response["user"]["created_at"] = $user["year_joined"];
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";
}
?>
登錄和註冊功能共享userfunction類:
public class UserFunctions {
private JSONParser jsonParser;
private static String loginURL = "http://10.0.2.2:8000/project/index.php";
private static String registerURL = "http://10.0.2.2:8000/project/index.php";
private static String login_tag = "login";
private static String register_tag = "register";
// constructor
public UserFunctions() {
jsonParser = new JSONParser();
}
// login with user provided email/pass
public JSONObject loginUser(String email, String password) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.makeHttpRequest(loginURL, "GET", params);
return json;
}
// register a new user
public JSONObject registerUser(String email, String password, String contact) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("contact", contact));
//params.add(new BasicNameValuePair("year", Integer.toString(year)));
// getting JSON Object
JSONObject json = jsonParser.makeHttpRequest(registerURL, "GET", params);
return json;
}
// determine if the user is logged in
public boolean isUserLoggedIn(Context context) {
DatabaseHandler db = new DatabaseHandler(context);
int count = db.getRowCount();
if (count > 0) {
// user logged in
return true;
}
return false;
}
// logout the user
public boolean logoutUser(Context context) {
DatabaseHandler db = new DatabaseHandler(context);
db.resetTables();
return true;
}
}
什麼在:$ DB-> storeUser($電子郵件,$接觸,$密碼); –
應該是來自這裏的輸入嗎? 'params.add(new BasicNameValuePair(「email」,email)); params.add(new BasicNameValuePair(「password」,password)); params.add(new BasicNameValuePair(「contact」,contact));' – Benz