我想在android中使用凌空創建一個登錄活動。我已經在localhost phpmyadmin上建立了我的數據庫。我已經成功創建了註冊活動,但每當我嘗試從我剛在數據庫中註冊的電子郵件和密碼登錄時,突然我的應用程序崩潰。Android登錄使用PHP,MySQL和Volley
,這裏是我的login.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$email = $_POST['email'];
$password = $_POST['password'];
require_once('connection.php');
$sql = "select * from person where email='$email' and password='$password'";
$check = mysqli_fetch_array(mysqli_query($connect,$sql));
if(isset($check)){
echo "success";
}else{
echo "Invalid Username or Password";
}
}else{
echo "error try again";
}
?>
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mememe.project">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity">
<intent-filter>
<action android:name="android.intent.action.RegisterActivity" />
</intent-filter>
</activity>
<activity android:name=".ProfileActivity"></activity>
</application>
</manifest>
LoginActivity.java
package com.example.mememe.project;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 LoginActivity extends AppCompatActivity implements View.OnClickListener {
EditText etEmail, etPassword;
Button login;
StringRequest request;
RequestQueue requestQueue;
public static final String EMAIL = "email";
public static final String PASSWORD = "password";
private static final String LOGIN_URL = "http://192.XXX.XX.XX/loginApp/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
requestQueue = Volley.newRequestQueue(this);
email = (EditText) findViewById(R.id.etEmail);
password = (EditText) findViewById(R.id.etPassword);
login = (Button) findViewById(R.id.logIn);
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
userLogin(email.getText().toString(), password.getText().toString());
}
private void userLogin(final String email, final String password){
StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if(response.trim().equals("success")){
Intent intent = new Intent();
intent.putExtra(EMAIL, email);
intent.putExtra(PASSWORD, password);
startActivity(intent);
}else{
Toast.makeText(LoginActivity.this, response, Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("email", email);
map.put("password", password);
return map;
}
};
requestQueue.add(request);
}
}
ProfileActivity.java
package com.example.mememe.project;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class ProfileActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
textView = (TextView) findViewById(R.id.textViewUserName);
Intent intent = getIntent();
textView.setText("welcome" + intent.getStringExtra(LoginActivity.EMAIL));
}
}
可以ü添加logcat的?我們需要知道它爲什麼崩潰的原因 –