I have a problem when registering a user in my android app
In the registry.java I have the following code
public class Registrarse extends AppCompatActivity {
EditText etnombre, etusuario, etpassword, etedad;
Button btn_registrar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registrarse);
etnombre = (EditText) findViewById(R.id.Registro_nombre);
etusuario = (EditText) findViewById(R.id.Registro_usuario);
etpassword = (EditText) findViewById(R.id.Registro_pass);
etedad = (EditText) findViewById(R.id.Registro_edad);
btn_registrar = (Button) findViewById(R.id.R_rgr);
btn_registrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etnombre.getText().toString();
String username = etusuario.getText().toString();
String password = etpassword.getText().toString();
int edad = Integer.parseInt(etedad.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(Registrarse.this, MainActivity.class);
startActivity(intent);
Toast.makeText(Registrarse.this, "Registration Successful", Toast.LENGTH_SHORT).show();
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(Registrarse.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegistroRequest registroRequest = new RegistroRequest(name, edad ,username, password, responseListener);
RequestQueue queue = Volley.newRequestQueue(Registrarse.this);
queue.add(registroRequest);
}
});
}
In the request.java register I have the following code:
public class RegistroRequest extends StringRequest{
private static final String REGISTER_REQUEST_URL="http://172.20.23.65/Register.php";
private Map<String, String> params;
public RegistroRequest(String name, int age, String username, String password, Response.Listener<String> listener){
super(Method.POST, REGISTER_REQUEST_URL, listener, null);
params = new HashMap<>();
params.put("name",name);
params.put("edad", age+"");
params.put("username",username);
params.put("password",password);
}
@Override
public Map<String, String> getParams() {
return params;
}
And my php code is as follows:
<?php
$con = mysqli_connect("localhost", "root", "", "usuarios");
$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);
The bug he gives me is this:
W / System.err: org.json.JSONException: Value% co_of% of type java.lang.String can not be converted to JSONObject