我正在尝试通过cPanel Hosting将我的项目应用程序与在线数据库连接。我在Android Studio中使用了AlertDialog.Builder,setOnClickListener,Try和Catch,但它不起作用。我正在使用这个登录页面。登录按钮不会将我重定向到Home活动。警报对话框也不会出现。
这是我的登录页面的Java文件,
package daffaalmer.bloom2nd;
import android.app.AlertDialog;
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 com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class Login extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText EditTextEmail = (EditText)
findViewById(R.id.EditTextEmail);
final EditText EditTextPassword = (EditText)
findViewById(R.id.EditTextPassword);
final Button ButtonLogin = (Button) findViewById(R.id.ButtonLogin);
final TextView TextViewRegister = (TextView)
findViewById(R.id.TextViewRegister);
TextViewRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent registerIntent = new Intent(Login.this, Register.class);
Login.this.startActivity(registerIntent);
}
});
ButtonLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = EditTextEmail.getText().toString();
final String password = EditTextPassword.getText().toString();
Response.Listener<String> responseListener = new
Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success =
jsonResponse.getBoolean("success");
if (success) {
String name = jsonResponse.getString("name");
String email = jsonResponse.getString("email");
Intent loginIntent = new Intent(Login.this,
Home.class);
loginIntent.putExtra("name", name);
loginIntent.putExtra("email", email);
Login.this.startActivity(loginIntent);
} else {
AlertDialog.Builder builder = new
AlertDialog.Builder(Login.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(email, password,
responseListener);
RequestQueue queue = Volley.newRequestQueue(Login.this);
queue.add(loginRequest);
}
});
}
}
这是我的登录页面的XML文件,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:paddingBottom="64dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="64dp"
tools:context="daffaalmer.bloom2nd.Login">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp">
<ImageView
android:id="@+id/BloomLogo"
android:layout_width="match_parent"
android:layout_height="150dp"
app:srcCompat="@drawable/logo"
android:layout_weight="0.26" />
<ImageView
android:id="@+id/WelcomeWriting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:srcCompat="@drawable/writing" />
<EditText
android:id="@+id/EditTextEmail"
style="@style/edit_text_style"
android:hint="E-mail Address"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/EditTextPassword"
style="@style/edit_text_style"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/ButtonLogin"
style="@style/button_style"
android:text="LOG IN" />
<TextView
android:id="@+id/TextViewRegister"
style="@style/textView_style"
android:text="Not a member yet? Sign Up"
android:textAppearance="@android:style/TextAppearance.Medium" />
</LinearLayout>
</RelativeLayout>
这是我的LoginRequest的Java文件,
package daffaalmer.bloom2nd;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class LoginRequest extends StringRequest {
private static final String LOGIN_REQUEST_URL =
"daffaalmerf.5gbfree.com/Login.php";
private Map<String, String> params;
public LoginRequest(String email, String password, Response.Listener<String>
listener){
super(Method.POST, LOGIN_REQUEST_URL, listener, null );
params = new HashMap<>();
params.put("email", email);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {return params;}
}
这是我在数据库中的Login.php文件
<?php
$con = mysqli_connect("daffaalmerf.5gbfree.com", "daffaalm_almer",
"daffaalm2849", "daffaalm_pribadi_data");
$email = $_POST["email"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM pribadi_data WHERE email =
? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $email, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $userID, $name, $dateofbirth, $address,
$phonenumber, $email, $password);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)){
$response["success"] = true;
$response["name"] = $name;
$response["dateofbirth"] = $dateofbirth;
$response["address"] = $address;
$response["phonenumber"] = $phonenumber;
$response["email"] = $email;
$response["password"] = $password;
}
echo json_encode($response);
?>