我正在尝试做一个Android应用程序,并且我需要登录活动。问题在于应用程序正在思考并且没有前进
这是我的登录按钮
btn_login.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String mEmail = email.getText().toString().trim();
String mPass = password.getText().toString().trim();
if (!mEmail.isEmpty() || !mPass.isEmpty()){
Login(mEmail,mPass);
}else{
email.setError("Insira email");
password.setError("Insira palavra-passe");
}
}
});
这是我使用stringrequest的登录方法,我正在使用Volley Library和JSON。
private void Login(final String mEmail, final String mPass) {
loading.setVisibility(View.VISIBLE);
btn_login.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(LoginActivity.this, "Bem-vindo: \n" +name+email,Toast.LENGTH_LONG ).show();
loading.setVisibility(View.GONE);
}
}
}catch(JSONException e){
e.printStackTrace();
loading.setVisibility(View.GONE);
btn_login.setVisibility(View.VISIBLE);
Toast.makeText(LoginActivity.this, "Erro: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
loading.setVisibility(View.GONE);
btn_login.setVisibility(View.VISIBLE);
Toast.makeText(LoginActivity.this, "Erro: " + error.toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> params = new HashMap<>();
params.put("email", mEmail);
params.put("password", mPass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
这是我的php代码:
if ($_SERVER['REQUEST_METHOD']=='POST') {
$email = $_POST['email'];
$password = $_POST['password'];
require_once 'connect.php';
$sql = "SELECT * FROM users_table 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'];
$index['id'] = $row['id'];
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);
}
}
}
我的意思是错误在php代码中,因为进度条显示并且永不消失。
感谢您的帮助
答案 0 :(得分:0)
在日志中跟踪在android中的Login方法中进行的API调用发生了什么情况,您可以检查是否收到任何响应或进行了错误的API调用 另外,如果您输入了错误的电子邮件ID,则没有任何反应