我正在尝试为应用创建登录页面,但我认为
if ($_SERVER['REQUEST_METHOD']=='POST')
满足条件。
我不确定这是由php还是android代码的问题引起的,因为我使用相同的代码进行了有效的注册活动。我想知道密码散列是否可能有问题?
登录时,尽管没有出现成功登录的提示,但进度栏仍会弹出。我不确定问题出在哪里,任何帮助将不胜感激。
带有if语句的php代码:
<?php
if ($_SERVER['REQUEST_METHOD']=='POST'){
require 'conn.php';
$email = $_POST['email'];
$password = $_POST['password'];
$sql = "SELECT * FROM Patients WHERE email = '$email'";
$response = mysqli_query($conn, $sql);
$result = array();
$result["login"] = array();
if (mysqli_num_rows($response) === 1 ) {
$row = mysqli_fetch_assoc($response);
if (password_verify($password, $row['password']) ) {
$index['name'] = $row['name'];
$index['email'] = $row['email'];
array_push($result['login'], $index);
$result['success'] = "1";
$result['message'] = "success";
echo json_encode($result);
mysqli_close($conn);
} else {
$result['success'] = "0";
$result['message'] = "error";
echo json_encode($result);
mysqli_close($conn);
}
}
}
?>
Java JSON解析代码:
private void Login(final String email, final String password) {
loading.setVisibility(View.VISIBLE);
btnLogin.setVisibility(View.GONE);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("login");
if(success.equals("1")){
for(int i = 0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String name = object.getString("name").trim();
String email = object.getString("email").trim();
Toast.makeText(Main2Activity.this, "Success login. \nYour name : "+name+"\nYour Email : "+email, Toast.LENGTH_SHORT).show();
loading.setVisibility(View.GONE);
}
}
} catch (JSONException e) {
e.printStackTrace();
loading.setVisibility(View.GONE);
btnLogin.setVisibility(View.VISIBLE);
Toast.makeText(Main2Activity.this, "Error"+e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.setVisibility(View.GONE);
btnLogin.setVisibility(View.VISIBLE);
Toast.makeText(Main2Activity.this, "Error"+error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
我没有收到任何错误消息,因此我认为onResponse方法无效。而是显示进度条,什么也没有发生。