1
I need to send a JSON object from my android app to my php api. How can I do this?
public void btnCadastrar(View view) throws JSONException, IOException {
final EditText edtNome = ((EditText) findViewById(R.id.edtNome));
final EditText edtSobrenome = ((EditText) findViewById(R.id.edtSobrenome));
EditText edtCelular = ((EditText) findViewById(R.id.edtCelular));
EditText edtCep = ((EditText) findViewById(R.id.edtCep));
EditText edtLogradouro = ((EditText) findViewById(R.id.edtLogradouro));
EditText edtBairro = ((EditText) findViewById(R.id.edtBairro));
EditText edtLocalidade = ((EditText) findViewById(R.id.edtCidade));
EditText edtUF = ((EditText) findViewById(R.id.edtUF));
final String nome = edtNome.getText().toString();
String sobrenome = edtSobrenome.getText().toString();
String celular = edtCelular.getText().toString();
String cep = edtCep.getText().toString();
String logradouro = edtLogradouro.getText().toString();
String bairro = edtBairro.getText().toString();
String cidade = edtLocalidade.getText().toString();
String uf = edtUF.getText().toString();
JSONObject dadosJsonObject = new JSONObject();
dadosJsonObject.put("nome", edtNome.getText().toString());
dadosJsonObject.put("sobrenome", edtSobrenome.getText().toString());
dadosJsonObject.put("celular", edtCelular.getText().toString());
dadosJsonObject.put("cep", edtCep.getText().toString());
dadosJsonObject.put("logradouro", edtLogradouro.getText().toString());
dadosJsonObject.put("bairro", edtBairro.getText().toString());
dadosJsonObject.put("cidade", edtLocalidade.getText().toString());
dadosJsonObject.put("uf", edtUF.getText().toString());
dadosJsonObject.toString();
//Toast.makeText(getBaseContext(),"Dados retornados: "+dadosJsonObject, Toast.LENGTH_LONG).show();
//String url = "http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php?dados="+dadosJsonObject;
URL url = new URL("http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php");
}
PHP:
<?php
header("Access-Control-Allow-Origin: *");
ini_set('display_errors', true);
error_reporting(E_ALL);
include_once("con.php");
$pdo = conectar();
$data = file_get_contents("php://input");
$data = json_decode($dadosJsonObject);
print_r($data);
?>
Okay, I’ll see to it.
– GustavoSevero
Man, I found a lot of stuff about Asynctask, but none that was clear, nothing that I understood
– GustavoSevero