Problems with json on android

1

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

    
asked by David Román Rey 09.04.2018 в 16:38
source

1 answer

0

What your code does is get the answer and try to convert it to JSONObject :

          @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonResponse = new JSONObject(response);
                    ... 
                    ...

The problem here is that the response is not JSON-content-specific and is not a JSON object, since it must start with { .

If the answer is not a JSON, you will get the error:

  

System.err: org.json.JSONException: Value% co_of% of type java.lang.String   can not be converted to JSONObject

In fact if you check the error, you are actually getting html content ( <br ).

    
answered by 09.04.2018 в 16:46