How do I send a JSON object from my android to php?

Asked

Viewed 273 times

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);
?>

1 answer

2

Roughly it would be something like

public class MyAsyncTask extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... params) {
        URL url = null;
        try {
            String path = (String) params[0];
            String json = (String) params[1];

            url = new URL(path);

            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setReadTimeout(30000);
            httpURLConnection.setConnectTimeout(30000);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            httpURLConnection.connect();

            OutputStream outputStream = httpURLConnection.getOutputStream();
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");

            outputStreamWriter.write(json); //dados a seram enviados

            outputStreamWriter.flush();
            outputStreamWriter.close();

            String inputLine = "";

            InputStreamReader streamReader = new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8");

            BufferedReader reader = new BufferedReader(streamReader);

            StringBuilder stringBuilder = new StringBuilder();

            while ((inputLine = reader.readLine()) != null) {
                stringBuilder.append(inputLine);
            }

            reader.close();
            streamReader.close();

            return stringBuilder.toString();

        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (ProtocolException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }
}

And called something like:

MyAsyncTask myAsyncTask = new MyAsyncTask();
myAsyncTask.execute("http://reservacomdomanda.com/areaAdmin/api/area_admin/usuario.php", dadosJsonObject.toString()); // trocar pelos seus dados

Remembering that I created this dummy Asynctak that will only work to exemplify. I suggest you take a look at AsynkTask to adapt to your needs

  • Okay, I’ll see to it.

  • Man, I found a lot of stuff about Asynctask, but none that was clear, nothing that I understood

Browser other questions tagged

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