2016-11-08 43 views
0

我是新來的android我一直在登錄頁面,並嘗試使用PHP和MySQL連接到數據庫,但它顯示致命錯誤「不幸的是應用程序沒有響應「android登錄頁面「不幸的應用程序是不respinding」錯誤

的login.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); 
} 

?>

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 

}

+0

添加堆棧跟蹤,如果有一個崩潰。 –

+0

您是否嘗試過在Android程序上使用調試器? – Tim

+0

不,我還沒有嘗試過使用調試器, –

回答

0

1)的問題可能是服務器端的問題,由於過時的設置,請確保您的PHP腳本中的所有文件的mysqli或PDO使用的,而不是棄用「msql」

2)確保您的所有活動都已添加到清單文件中。

3)確保您的手機或測試設備連接到與您的計算機相同的wifi。

4)如果上述不起作用,我會幫助重寫你的代碼。

快樂編碼

+0

我已經嘗試了所有這些方法,但我仍然面臨同樣的問題 –

相關問題