Parameter error always null

0

I'm trying to make a register system when I click the button, everything happens but the parameter is always getting null. (Even though I define it 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();
        }
    }
}

Button:

 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;
}
?>
    
asked by anonymous 21.12.2017 / 01:53

0 answers