答案 0 :(得分:0)
我建议您使用Volley Library来实现登录 - 注册活动。 这是Android开发的最新趋势,比过去的方法更容易。 我将举例说明注册活动:
我的register.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/activity_horizontal_margin"
android:background="@drawable/icon13">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:maxLength="16"
android:id="@+id/etName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Όνομα" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etUsername"
android:maxLength="16"
android:layout_below="@+id/etName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Όνομα Χρήστη" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/etPassword"
android:maxLength="16"
android:layout_below="@+id/etUsername"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Κωδικός" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/etAge"
android:maxLength="3"
android:layout_below="@+id/etPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Ηλικία" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Εγγραφή"
android:id="@+id/bRegister"
android:layout_below="@+id/etAge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/etAge"
android:layout_alignEnd="@+id/etAge"
android:layout_gravity="center"
style="@style/Buttonlogo"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Πίσω"
android:id="@+id/backRegister"
android:layout_below="@+id/etAge"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/etAge"
android:layout_alignEnd="@+id/etAge"
android:layout_gravity="center"
android:layout_margin="20dp"
android:onClick="OnClickBackButton"
style="@style/Buttonlogo"/>
</LinearLayout>
我的RegisterActivity.java:
package com.example.sakis.loginregister;
import android.app.AlertDialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
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 RegisterActivity extends AppCompatActivity {
/*** Binary Semaphore lock is used to prevent fast-click. ***/
int lock=1;
String username="";
MediaPlayer md;
int age;
String age_of_player;
String password="";
String name="";
Handler mHandler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
md = MediaPlayer.create(RegisterActivity.this, R.raw.tick);
final EditText etAge = (EditText) findViewById(R.id.etAge);
final EditText etName = (EditText) findViewById(R.id.etName);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bRegister = (Button) findViewById(R.id.bRegister);
bRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (lock == 1) {
md.release();
md = MediaPlayer.create(RegisterActivity.this, R.raw.tick);
md.start();
lock = 0;
name = etName.getText().toString();
username = etUsername.getText().toString();
age_of_player = etAge.getText().toString();
password = etPassword.getText().toString();
if (username.matches("") || name.matches("") || password.matches("") || age_of_player.matches("")) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(RegisterActivity.this,R.style.myBackgroundStyle);
builder1.setMessage("Συμπλήρωσε όλα τα πεδία")
.setNegativeButton("Ξαναπροσπάθησε", null)
.create()
.show();
Runnable runnable = new Runnable() {
@Override
public void run() {
lock = 1;
}
};
mHandler.postDelayed(runnable, 1000);
} else {
age = Integer.parseInt(age_of_player);
/***Insert new user to Database. ***/
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) {
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(intent);
finish();
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this,R.style.myBackgroundStyle);
builder.setMessage("Όνομα χρήστη κατειλημμένο")
.setNegativeButton("Ξαναπροσπάθησε", null)
.create()
.show();
/***Set lock back to value '1' .***/
Runnable runnable = new Runnable() {
@Override
public void run() {
lock = 1;
}
};
mHandler.postDelayed(runnable, 1000);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, username, age, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
}
}
});
}
public void OnClickBackButton(View view) {
if(lock==1) {
md.release();
md = MediaPlayer.create(RegisterActivity.this, R.raw.tick);
md.start();
lock=0;
Intent backregisterintent = new Intent(this, LoginActivity.class);
startActivity(backregisterintent);
finish();
}
}
/*** Mobile-phone back button method .***/
public void onBackPressed() {
if(lock==1) {
md.release();
md = MediaPlayer.create(RegisterActivity.this, R.raw.tick);
md.start();
lock=0;
Intent backregisterintent = new Intent(this, LoginActivity.class);
startActivity(backregisterintent);
finish();
}
}
}
我的RegisterRequest.java类:
package com.example.sakis.loginregister;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://Your_Server_Name/Register.php";
private Map<String, String> params;
public RegisterRequest(String name, String username, int age, String password, Response.Listener<String> listener) {
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("name", name);
params.put("username", username );
params.put("age", age + "");
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
我的Register.php:
<?php
$connect = mysqli_connect("mysql.hostinger.gr", "username", "password", "database_name");
$name = $_POST["name"];
$username = $_POST["username"];
$age = $_POST["age"];
$password = $_POST["password"];
function registerUser() {
global $connect, $name, $age, $username, $password;
$statement = mysqli_prepare($connect, "INSERT INTO User (name,username,age, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "ssis", $name, $username, $age, $password);
mysqli_stmt_execute($statement);
mysqli_stmt_close($statement);
};
function usernameAvailable() {
global $connect, $username;
$statement = mysqli_prepare($connect, "SELECT * FROM User WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
$count = mysqli_stmt_num_rows($statement);
mysqli_stmt_close($statement);
if ($count < 1){
return true;
}
else {
return false;
}
};
$response = array();
$response["success"] = false;
if (usernameAvailable()){
registerUser();
$response["success"] = true;
}
print_r(json_encode($response));
?>
您可以根据自己的需要编辑我的示例。您也可以一步一步地遵循这个好的教程: https://www.youtube.com/watch?v=QxffHgiJ64M&list=PLe60o7ed8E-TztoF2K3y4VdDgT6APZ0ka