android登录页面“不幸的是,应用程序不响应”错误


android sign in page "Unfortunately app is not respinding "error

我是android的新手,我一直在登录页面上工作,并试图使用PHP和MySQL将其连接到数据库,但它显示致命错误"不幸的是,应用程序没有响应"

login。

<?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);
}

?>

Signin.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)确保你所有的活动都被添加到你的manifest文件中。

3)确保您的手机或测试设备连接到与计算机相同的wifi。

如果以上没有帮助,我将帮助你重写代码。 <<p> 快乐编码/em>
相关文章: