RegisterActivity.java
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
final EditText etname = (EditText) findViewById(R.id.reg_name);
final EditText etage = (EditText) findViewById(R.id.reg_age);
final EditText etusername = (EditText) findViewById(R.id.reg_username);
final EditText etpass = (EditText) findViewById(R.id.reg_pass);
Button reg_btn = (Button) findViewById(R.id.btn_reg);
reg_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etname.getText().toString();
int age = Integer.parseInt(etage.getText().toString());
String username = etusername.getText().toString();
String password = etpass.getText().toString();
Response.Listener<String> responselistener = new Response.Listener<String>() {
@Override
/*reponse from register.php*/
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if(success){
Toast.makeText(RegisterActivity.this, "Registration successful!", Toast.LENGTH_LONG).show();
Intent i = new Intent(RegisterActivity.this, LoginActivity.class);
RegisterActivity.this.startActivity(i);
}else{
/* Memunculkan dialog box berisi kata "Register Failed" dan tombol "Retry" agar
pengguna dapat mencoba mendaftar kembali
*/
AlertDialog.Builder builder = new AlertDialog.Builder(RegisterActivity.this);
builder.setMessage("Register Failed").setNegativeButton("Retry", null).create().show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(name, age, username, password, responselistener);
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
RegisterRequest.java
public class RegisterRequest extends StringRequest {
private static String REGISTER_REQUEST_URL = "http://tesapp.000webhostapp.com/Register.php";
private Map<String, String> params;
public RegisterRequest(String name, int age, String username, String password, Response.Listener<String> listener){
/* Method.POST untuk mengirim data ke register.php dan register.php akan merespon data tersebut */
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("name", name);
params.put("age", age + "");
params.put("username", username);
params.put("password", password);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
Register.php
<?php
$con = mysqli_connect("localhost", "id406711_apmob", "animasi", "id406711_userdata");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (name, age, username, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $age, $username, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
当我运行应用程序时,我收到了这个错误:
D / NetworkSecurityConfig:未使用平台默认设置指定网络安全配置 E / Volley:[86] BasicNetwork.performRequest:http://tesapp.000webhostapp.com/Register.php的意外响应代码200
请帮助......:)