How to send data via POST and retrieve in PHP?

6

How do I send the data via POST with the 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();
            }
        }
    });
}
    
asked by anonymous 19.06.2015 / 22:14

1 answer

1

The only parameter you did not send was method , in case I believe that valores.add(new BasicNameValuePair("method", "send-json")); is missing

The code should 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();
            }
        }
    });
}
    
21.06.2015 / 17:59