Hi guys, I am new to android and I am trying to develop my first app, I started with this tutorial and got to the step #4 so far
I got to the point of interaction between the app and the database but here something happens, specifically when I click the registerHere link, the app crashes and I don't know whats going on. I have tried it in my phone and also in the virtual nexus 5 and on both is the same result.
The Host provider I am using is https://profreehost.com/ there I created a SQL table.
Please if you can give me your support to see how can I fix this issue. Thanks!
The register request.java
For the register activity.Java
Also the php file Register.php
I got to the point of interaction between the app and the database but here something happens, specifically when I click the registerHere link, the app crashes and I don't know whats going on. I have tried it in my phone and also in the virtual nexus 5 and on both is the same result.
The Host provider I am using is https://profreehost.com/ there I created a SQL table.
Please if you can give me your support to see how can I fix this issue. Thanks!
The register request.java
Code:
package com.example.android.loginregister;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
/**
* Created by DeadMarshes on 7/21/2017.
*/
public class RegisterRequest extends StringRequest {
private static final String REGISTER_REQUEST_URL = "http://danielandnorman.unaux.com/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("password", password);
params.put("age", age + "");
}
[USER=1021285]@override[/USER]
public Map<String, String> getParams() {
return params;
}
For the register activity.Java
Code:
public class RegisterActivity extends AppCompatActivity {
[USER=1021285]@override[/USER]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
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() {
[USER=1021285]@override[/USER]
public void onClick(View view) {
final String name = etName.getText().toString();
final String username = etUsername.getText().toString();
final String password = etPassword.getText().toString();
final int age = Integer.parseInt(etAge.getText().toString());
Response.Listener<String> responseListener = new Response.Listener<String>() {
[USER=1021285]@override[/USER]
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);
} else{
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, username,age, password, responseListener );
RequestQueue queue = Volley.newRequestQueue(RegisterActivity.this);
queue.add(registerRequest);
}
});
}
}
Also the php file Register.php
Code:
<?php
$con = mysqli_connect("sql111.unaux.com", "username", "password", "table");
$name = $_POST["name"];
$age = $_POST["age"];
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO UserTable (name, username, age, password) VALUES (?, ?, ?, ?)");
mysqli_stmt_bind_param($statement, "siss", $name, $username, $age, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;
echo json_encode($response);
?>
Last edited by a moderator: