Android + Volley with PHP connector returns error!

Asked

Viewed 58 times

1

I am trying to connect and send a registration, via PHP Nector, from my Android application to SQL Server on the local computer. It’s returning "Error!" because it looks like Volley doesn’t see the Nector, or something like that. Could you help me with what I’m missing?

public class Cadastro extends AppCompatActivity{
EditText txtNome, txtEmail, txtSenha, txtRepeteSenha;
Button btnCadastrar;
ProgressBar progressBar;
ConnectionClass connectionClass;

String url = "http://localhost/cadastro.php";
AlertDialog.Builder alertDialog;

@Override
protected void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro);
    connectionClass = new ConnectionClass();
    txtNome = findViewById(R.id.name);
    txtEmail = findViewById(R.id.mail);
    txtSenha = findViewById(R.id.key);
    txtRepeteSenha = findViewById(R.id.key_again);
    btnCadastrar = findViewById(R.id.cadastrar);
    progressBar = findViewById(R.id.progressBar);

    btnCadastrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String nome, email, senha;
            nome = txtNome.getText().toString();
            email = txtEmail.getText().toString();
            senha = txtSenha.getText().toString();
            RequestQueue queue = Volley.newRequestQueue(Cadastro.this);

            StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            alertDialog = new AlertDialog.Builder(Cadastro.this);
                            alertDialog.setTitle("Resposta do servidor:");
                            alertDialog.setMessage("Resposta: " + response);
                            alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    txtNome.setText("");
                                    txtEmail.setText("");
                                    txtSenha.setText("");
                                }
                            });
                            AlertDialog alertDialog2 = alertDialog.create();
                            alertDialog2.show();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Cadastro.this, "Erro!", Toast.LENGTH_SHORT).show();
                    error.printStackTrace();
                }
            }){
                @Override
                protected Map<String, String> getParams() {
                    Map<String, String> params = new HashMap<>();
                    params.put("nome", nome);
                    params.put("email", email);
                    params.put("senha", senha);
                    return params;
                }
            };
            queue.add(stringRequest);
        }
    });
}

2 answers

0

I switched the "localhost" for the IP and worked the connection and the search, however I still can not enter data in SQL Server. Here is my code below:

<?php
include_once "conexao.php";

$username = "";   
$email = "";    
$password = ""; 
if(isset($_POST['nome'])){    
    $username = $_POST['nome'];   
}
if(isset($_POST['email'])){    
    $email = $_POST['email'];    
}
if(isset($_POST['senha'])){    
    $password = $_POST['senha'];    
}
if(!empty($username) && !empty($password) && !empty($email)){
    $sql1 = "INSERT INTO Usuario (nome, email, senha) VALUES ('".$_POST['nome']."','".$_POST['email']."','".$_POST['senha']."')";
}
include_once "busca.php";
?>
  • Hello Paul, this does not answer your question. You wrote in a place of answers. I advise you to create a new question about the specific problem that is the insertion.

  • Your question refers to the connection problem between android and php.

  • ok. Leave it to me, create a new post here. Thank you so far.

0


The device (emulator) can not connect because you need to insert the IP instead of "localhost" because in "localhost" the command will try to make an internal connection in "mobile" (emulator) and not on the server.

Alter:

String url = "http://localhost/cadastro.php";

To:

String url = "http://999.000.0.00/cadastro.php"; // por exemplo

How do I recover this IP?

Typo ipconfig in cmd (windows) and it will bring several addresses. I use Ipv4:

inserir a descrição da imagem aqui

I answered based on this part of the question too:

[...]'Cause it looks like Volley doesn’t see the Nector[...]

  • 1

    Something that only practice brings. Something simple but not thought out by a beginner like me. Thank you very much. I switched and it worked, but I didn’t know why it worked. Now I understand why. Now I’m going after the.php register, which is giving problem, if you can help, thank you. At the end, when you finish the little project, I put all the code here.

  • @Paulolara tranquil... Approve the answer if it is correct.

  • @Paulolara your problem with regards to Insert is very simple to solve.

Browser other questions tagged

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