gpt4 book ai didi

java - android登录页面 "Unfortunately app is not respinding "错误

转载 作者:行者123 更新时间:2023-11-29 20:02:06 25 4
gpt4 key购买 nike

我是 Android 新手,我一直在开发登录页面并尝试使用 PHP 和 MySQL 将其连接到数据库,但它显示 fatal error “不幸的是应用程序没有响应”

登录.php

<?php 

if($_SERVER['REQUEST_METHOD']=='POST'){
//Getting values
$username = $_POST['email'];
$password = $_POST['password'];

//Creating sql query
$sql = "SELECT * FROM users WHERE email='$username' AND password='$password'";

//importing dbConnect.php script
require_once('dbConnect.php');

//executing query
$result = mysqli_query($con,$sql);

//fetching result
$check = mysqli_fetch_array($result);

//if we got some result
if(isset($check)){
//displaying success
echo "success";
}else{
//displaying failure
echo "failure";
}
mysqli_close($con);
}

?>

登录.java

 import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.widget.Button ;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.HashMap;
import java.util.Map;

public class Signin extends AppCompatActivity implements View.OnClickListener {

//Defining views
private EditText editTextEmail;
private EditText editTextPassword;
public Button bsignin;
final TextView textviewregister = (TextView) findViewById(R.id.textviewregister);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);



editTextEmail = (EditText) findViewById(R.id.editTextEmail);
editTextPassword = (EditText) findViewById(R.id.editTextPassword);
bsignin = (Button) findViewById(R.id.bRegister);
//bsignin = (AppCompatButton) findViewById(R.id.bsignin);



bsignin.setOnClickListener(this);
}

@Override
protected void onResume() {
super.onResume();
//In onresume fetching value from sharedpreference
SharedPreferences sharedPreferences = getSharedPreferences(SigninRequest.SHARED_PREF_NAME,Context.MODE_PRIVATE);

//Fetching the boolean value form sharedpreferences
boolean loggedIn = sharedPreferences.getBoolean(SigninRequest.LOGGEDIN_SHARED_PREF, false);

//If we will get true
if(loggedIn){
//We will start the Profile Activity
Intent intent = new Intent(Signin.this, Profile.class);
startActivity(intent);
}
}

private void login(){
//Getting values from edit texts
final String email = editTextEmail.getText().toString().trim();
final String password = editTextPassword.getText().toString().trim();

//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.POST, SigninRequest.LOGIN_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//If we are getting success from server
if(response.equalsIgnoreCase(SigninRequest.LOGIN_SUCCESS)){
//Creating a shared preference
SharedPreferences sharedPreferences = Signin.this.getSharedPreferences(SigninRequest.SHARED_PREF_NAME, Context.MODE_PRIVATE);

//Creating editor to store values to shared preferences
SharedPreferences.Editor editor = sharedPreferences.edit();

//Adding values to editor
editor.putBoolean(SigninRequest.LOGGEDIN_SHARED_PREF, true);
editor.putString(SigninRequest.EMAIL_SHARED_PREF, email);

//Saving values to editor
editor.apply();

//Starting profile activity
Intent intent = new Intent(Signin.this, Profile.class);
startActivity(intent);
}else{
//If the server response is not success
//Displaying an error message on toast
Toast.makeText(Signin.this, "Invalid username or password", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//You can handle error here if you want
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
//Adding parameters to request
params.put(SigninRequest.KEY_EMAIL, email);
params.put(SigninRequest.KEY_PASSWORD, password);

//returning parameter
return params;
}
};

textviewregister.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
Intent intent = new Intent(Signin.this,Signup.class);
startActivity(intent);

}
});

//Adding the string request to the queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}


@Override
public void onClick(View v) {
//Calling the login function
login();
}

}

SigninRequest.java

public class SigninRequest {
public static final String LOGIN_URL = "http://localhost/Login.php/";


public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";

//If server response is equal to this that means login is successful
public static final String LOGIN_SUCCESS = "success";


public static final String SHARED_PREF_NAME = "Bonpost";

//This would be used to store the email of current logged in user
public static final String EMAIL_SHARED_PREF = "email";

//We will use this to store the boolean in sharedpreference to track user is loggedin or not
public static final String LOGGEDIN_SHARED_PREF = "loggedin";
enter code here

}

最佳答案

1) 问题可能是由于不推荐使用的设置而导致的服务器端问题,请确保您的 php 脚本在所有文件中使用 mysqli 或 pdo 而不是不推荐使用的“msql”

2) 确保您的所有 Activity 都添加到 list 文件中。

3) 确保您的手机或测试设备与计算机连接到同一 Wi-Fi。

4)如果上述方法没有帮助,我会帮助重写您的代码。

快乐编码

关于java - android登录页面 "Unfortunately app is not respinding "错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40491645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com