下面是我的用户注册注册类,在用户点击注册的类中,它从字段中获取信息并将其传递给注册请求(下面发布的注册请求代码),我没有添加任何内容在android studio中执行我的代码后的表。
package com.example.user.emaillogin;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 Register extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etemail =(EditText) findViewById(R.id.etemail);
final EditText etpassword =(EditText) findViewById(R.id.etpassword);
final EditText etname =(EditText) findViewById(R.id.etname);
final Button buttonregister =(Button) findViewById(R.id.buttonregister);
buttonregister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String name = etname.getText().toString();
final String email = etemail.getText().toString();
final String password = etpassword.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){
Intent intent = new Intent(Register.this,MainActivity.class);
Register.this.startActivity(intent);
} else{
AlertDialog.Builder builder = new AlertDialog.Builder(Register.this);
builder.setMessage("Register failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
Registerrequest registerRequest = new Registerrequest(name, email, password, ResponseListener);
RequestQueue queue = Volley.newRequestQueue(Register.this);
queue.add(registerRequest);
}
});
}
}
这个Register Request类允许我向服务器上的register.php(下面发布的代码)文件发出请求,并以字符串形式获取响应,并将php文件上传到我的服务器
package com.example.user.emaillogin;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
/**
* Created by User on 07/02/2017.
*/
public class Registerrequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://picandlaugh.co.uk/register.php";
private Map<String, String> params;
public Registerrequest(String name, String email, String password, Response.Listener<String> listener){
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("name",name);
params.put("email",email);
params.put("password",password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
下面是我的register.php代码,我确实在实际代码中包含了实际的主机,用户名,密码等,但出于安全目的将其删除,
<?php
$con = mysqli_connect("HOST", "USERNAME", "PASSWORD", "DB");
$name = $_POST["name"];
$email = $_POST["mail"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, " INSERT INTO WINKEL (name, mail, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $name, $email, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>