Java Android索引php文件未注册用户
我目前正在关注Android Hive如何使用PHP和MYSQL创建登录和注册系统;但是,我在注册用户时遇到了一些困难 编辑:我的问题是,无论何时单击register按钮,它都会返回“Required tag is missing”,并且不会按应有的方式存储任何数据 我曾尝试使用谷歌浏览器的高级Rest客户端扩展,但它并没有真正为我提供任何有用的信息Java Android索引php文件未注册用户,java,php,android,mysql,sqlite,Java,Php,Android,Mysql,Sqlite,我目前正在关注Android Hive如何使用PHP和MYSQL创建登录和注册系统;但是,我在注册用户时遇到了一些困难 编辑:我的问题是,无论何时单击register按钮,它都会返回“Required tag is missing”,并且不会按应有的方式存储任何数据 我曾尝试使用谷歌浏览器的高级Rest客户端扩展,但它并没有真正为我提供任何有用的信息 index.php文件包含用于存储和从数据库获取用户数据的所有函数: <?php //this file has the role fo
index.php
文件包含用于存储和从数据库获取用户数据的所有函数:
<?php
//this file has the role fo accepting requests and giving JSON responses.
// Accepts POST and GET requests
if (isset($_POST['tag']) && $_POST['tag'] != ''){
//get the tag
$tag = $_POST['tag'];
//include a database handler
require_once 'login_api/DB_Functions.php';
$db = new DB_Functions();
//reponse array
$response = array("tag" => $tag, "error" => FALSE);
//check for the tag type
if ($tag == 'login'){
//request type is going to be to check the login
$email = $_POST['email'];
$password = $_POST['password'];
//check for the user
$user = $db->getUsersByEmailAndPassword($email, $password);
if ($user != false){
//this mean the user has been found
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created"] = $user["created"];
$response["user"]["updated"] = $user["updated"];
echo json_encode($response);
}else {
//user is not found
//echo the json with error = 1
$response["error"] = TRUE;
$response["error_msg"] = "Incorrect email or password";
echo json_response($response);
}
}else if ($tag == 'register') {
//request type is to register a new user
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
//check to see if the user already exists
if ($db->isUserExisted($email)){
//user already exists so produce an error response
$response["error"] = TRUE;
$response["error_msg"] = "User Already Exists";
echo json_encode($response);
}else{
//store user
$user = $db->storeUser($name, $email, $password);
if ($user){
//user is stored successfully
//Creating the data for JSON
$response["error"] = FALSE;
$response["uid"] = $user["unique_id"];
$response["user"]["name"] = $user["name"];
$response["user"]["email"] = $user["email"];
$response["user"]["created"] = $user["created"];
$response["user"]["updated"] = $user["updated"];
echo json_encode($response);
}else{
//failure storing the user details into the database
$response["error"] = TRUE;
$response["error_msg"] = "An error has occured with registering..";
echo json_encode($response);
}
}
}else {
//user failed to store
$response["error"] = TRUE;
$response["error_msg"] = "Uknown 'tag', should be either login or register";
echo json_encode($response);
}
}else {
$response["error"] = TRUE;
$response["error_msg"] = "Required, 'tag' is missing";
echo json_encode($response);
}
?>
那么你的问题是什么呢?似乎你的getParams()
函数由于某种原因没有被调用。@DanielNugent是的,我不太清楚为什么,我仔细地按照步骤操作,甚至下载了他的代码,但我不知道我的代码有什么问题。我不是一个非常熟练的android程序员,所以找到原因对我来说是一件非常痛苦的事
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
inputFullName = (EditText) findViewById(R.id.name);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// SQLite database handler
db = new SQLiteHandler(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(RegisterActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Please enter your details!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Login Screen
btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
finish();
}
});
}
/**
* Function to store user in MySQL database will post params(tag, name,
*/
private void registerUser(final String name, final String email,
final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created = user
.getString("created");
// Inserting row in users table
db.addUser(name, email, uid, created);
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "register");
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}