How to send data via POST and recover in a PHP?

Asked

Viewed 2,358 times

6

How to send the data via POST with send-json method, value 0, following my PHP code:

// Recupera os dados
$nomeUsuario = $_POST['nome'];
$cpfUsuario = $_POST['cpf'];
$bairroUsuario = $_POST['bairro'];
$emailUsuario = $_POST['email'];
$telefoneUsuario = $_POST['telefone'];

if(strcmp('send-json', $_POST['method']) == 0){ 

    // gravando em um arquivo de texto
    $f = fopen('CONFIG.TXT', 'a');
    $id = uniqid( time() );
    fwrite($f, 'ID: '.$id."\r\n");
    fwrite($f, 'Nome: '.$_POST['nome']."\r\n");
    fwrite($f, 'Cpf: '.$_POST['cpf']."\r\n");
    fwrite($f, 'Bairro: '.$_POST['bairro']."\r\n");
    fwrite($f, 'E-mail: '.$_POST['email']."\r\n");
    fwrite($f, 'Telefone: '.$_POST['telefone']."\r\n\r\n");

    fclose($f);

    echo 'Dados enviados com sucesso';

} 

Code, in java, to send the post:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.cordeiro-it.com.br/SOUPROGRESSO/Ctrl/novoUsuario.php");
System.out.println(telefone);
try{
    ArrayList<NameValuePair> valores = new ArrayList<NameValuePair>();
    valores.add(new BasicNameValuePair("nome", nome));
    valores.add(new BasicNameValuePair("cpf", cpf));
    valores.add(new BasicNameValuePair("bairro", bairro));
    valores.add(new BasicNameValuePair("email", email));
    valores.add(new BasicNameValuePair("telefone", telefone));

    httpPost.setEntity(new UrlEncodedFormEntity(valores));
    final HttpResponse resposta = httpClient.execute(httpPost);

    runOnUiThread(new Runnable(){
        public void run(){
            try {
                Toast.makeText(getBaseContext(), EntityUtils.toString(resposta.getEntity()), Toast.LENGTH_SHORT).show();
                Intent it = new Intent(getBaseContext(), NavigatorActivity.class);
                startActivity(it);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}
  • Type I want to know how to pass the value 0 to know that it is a new user. If it is value 1 it edits, if it is zero it saves

  • Hi Felipe. If you set up a webservice would not be easier?

  • He doesn’t get into the if that’s the problem?

1 answer

1

The only parameter you did not submit was the method, in case I believe it lacks valores.add(new BasicNameValuePair("method", "send-json"));

The code must be something like:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://www.cordeiro-it.com.br/SOUPROGRESSO/Ctrl/novoUsuario.php");
System.out.println(telefone);
try{
    ArrayList<NameValuePair> valores = new ArrayList<NameValuePair>();
    valores.add(new BasicNameValuePair("nome", nome));
    valores.add(new BasicNameValuePair("cpf", cpf));
    valores.add(new BasicNameValuePair("bairro", bairro));
    valores.add(new BasicNameValuePair("email", email));
    valores.add(new BasicNameValuePair("telefone", telefone));

    //Seu metodo
    valores.add(new BasicNameValuePair("method", "send-json"));

    httpPost.setEntity(new UrlEncodedFormEntity(valores));
    final HttpResponse resposta = httpClient.execute(httpPost);

    runOnUiThread(new Runnable(){
        public void run(){
            try {
                Toast.makeText(getBaseContext(), EntityUtils.toString(resposta.getEntity()), Toast.LENGTH_SHORT).show();
                Intent it = new Intent(getBaseContext(), NavigatorActivity.class);
                startActivity(it);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
}

Browser other questions tagged

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