Error parameter always null

Asked

Viewed 50 times

0

I’m trying to make a registration system when I click the button, everything goes right but the parameter is getting null always. (Even if I define in a string)

String url = "";
String parametros = "";

Connection:

public static String postDados(String urlUsuario, String parametrosUsuario){
    URL url;
    HttpURLConnection connection = null;
    try {
        url = new URL(urlUsuario);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlenconded");
        connection.setRequestProperty("Content-Length", Integer.toString(parametrosUsuario.getBytes().length));
        connection.setRequestProperty("Content-Language", "pt-BR, en-US");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);

        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        outputStreamWriter.write(parametrosUsuario);
        outputStreamWriter.flush();

        InputStream inputStream = connection.getInputStream();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        String linha;
        StringBuffer resposta = new StringBuffer();

        while ((linha = bufferedReader.readLine()) != null) {
            resposta.append(linha);
            resposta.append('\r');
        }
        bufferedReader.close();
        return resposta.toString();
    }catch (Exception e){
        return null;
    }
    finally {
        if (connection !=null){
            connection.disconnect();
        }
    }
}

Knob:

 Button registrar = findViewById(R.id.button2);
        registrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = "teste";
                url = "http://localhost/registro.php";
                parametros = "email="+email;
                new SolicitaDados().execute(url);
            }
        });

Request data:

private class SolicitaDados extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        return Conexao.postDados(urls[0], parametros);
    }

    @Override
    protected void onPostExecute(String result) {
    }
}

PHP:

<?php 
$servername = "localhost";
$username = "localhost";
$password = "localhost";
$dbnome = "localhost";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbnome);

    $email=$_POST['email'];

    $sql = "INSERT INTO Usuarios (Email)
VALUES ('$email')";

if ($conn->query($sql) === TRUE) {
    echo "Conta criada";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
  • You are sending the value this way String parametros = "[email protected]";?

  • Yes, in case [email protected]

  • Looking over, I see no flaw. So check, with a System.out.println(), if the parametrosUsuario within the postDados() is null. If it is, the problem is in the method call in Java. If it is not, it can be in PHP or the way you are writing in Outputstream.

  • I had to do it differently, but thanks for trying to help.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.